我使用 alamofire 将 30 MB 的 zip 文件上传到服务器
这是我的要求
let configuration = URLSessionConfiguration.background(withIdentifier: "identifire.com")
alamoFireManager = Alamofire.SessionManager(configuration: configuration)
let tempZipFilePath = UtilDirectory.tempZipPath.tweak()
alamoFireManager.upload(URL(fileURLWithPath: tempZipFilePath),
to: deligate.url,
method: .post,
headers: headers)
.uploadProgress(closure: {
progress in
print("Upload Progress: \(progress.fractionCompleted)")
})
.validate()
.responseJSON {}
当请求开始时,我可以看到上传进度 uploadProgress(closure:_)
,但如果我关闭应用程序,上传就会暂停。
我希望在关闭应用程序后,请求必须在不中断的情况下完成执行。
但为什么后台上传请求停止了?
最佳答案
您必须使用后台任务。你的应用程序仍然可能会被杀死。这取决于操作系统。看看这个https://developer.apple.com/documentation/uikit/uiapplication/1623031-beginbackgroundtask
来自 UIApplication 文档:
Use your UIApplication object to do the following...Extend the execution of the app so that it can finish a task in the background (beginBackgroundTask(expirationHandler:), beginBackgroundTask(withName:expirationHandler:))
关于swift - 为什么上传 alamofire 后台请求不在后台执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42042860/