swift - 为什么上传 alamofire 后台请求不在后台执行?

标签 swift xcode alamofire

我使用 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/

相关文章:

ios - 使用 URLRequestConvertible 在单元测试中链接错误

ios - 使用 Codable 转换为对象

ios - 如何在 Realm Swift 上查询 List<Int>

swift - 在 Safari 扩展中打开没有工具栏的 Safari 窗口

arrays - 我不明白为什么我的代码适用于我的结构数组,但不适用于包含此结构数组的结构?

xcode - 如何在 Windows 上安装 Armadillo?

ios - 有没有办法让 "force"键盘以编程方式在 iOS Swift 中关闭?

xcode6 swift NSObject AnyObject 错误

ios - Parse.com 查询超过 1000 个对象

ios - 如何分别使用 Alamofire 发送参数和正文?