我使用PDF
将Alamofire
文件上传到服务器。我已经创建backgroundSessionManager
以确保应用在应用程序处于后台时上传文件。当我从backgroundCompletionHandler
方法AppDelegate
调用handleEventsForBackgroundURLSession
时,代码完美无缺。
进展是我正在显示进度,当应用程序进入后台时,进度卡住了,.uploadProgress
完成也没有调用。因此,当用户进入后台并且在返回一段时间后,进度不会增加。
共享实例:
class Networking {
static let sharedInstance = Networking()
public var backgroundSessionManager: Alamofire.SessionManager // your web services you intend to keep running when the system backgrounds your app will use this
private init() {
self.backgroundSessionManager = Alamofire.SessionManager(configuration: URLSessionConfiguration.background(withIdentifier: "com.dw.myapp"))
var backgroundCompletionHandler: (() -> Void)? {
get {
return backgroundSessionManager.backgroundCompletionHandler
}
set {
backgroundSessionManager.backgroundCompletionHandler = newValue
}
}
}
}
上传文件代码
Networking.sharedInstance.backgroundSessionManager.upload(multipartFormData: { (multipartData) in
}, usingThreshold: UInt64.init(), to: "\(url)", method: .post, headers: headers) { (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
//Does not call while app is in background
Helper.dispatchMain {
print(progress)
print("Upload Time ", Date())
}
})
upload.responseJSON { response in
print("Response Time ", Date())
print("Response Time ", Date())
}
case .failure(let error):
completion!(["message" : error],false)
}
}
我搜索了SO和github页面但是没有找到解决方案。如果我犯了任何错误,任何人都可以指导吗?