【问题标题】:How to handle background uploads with Alamofire 5?如何使用 Alamofire 5 处理后台上传?
【发布时间】:2020-09-17 10:04:15
【问题描述】:

后台上传有可能通过以下代码对 alamofire api 和 URLSession 进行一些修改:

Alamofire.upload(
                multipartFormData: { multipartFormData in
                    multipartFormData.append(imageData, withName: "photo[image]", fileName: filename, mimeType: "image/jpg")
            },
                usingThreshold: UInt64(0), // force alamofire to always write to file no matter how small the payload is
                to: "http://", // if we give it a real url sometimes alamofire will attempt the first upload. I don't want to let it get to our servers but it fails if I feed it ""
                method: .post,
                headers: headers,
                encodingCompletion: { encodingResult in
                    switch encodingResult {
                    case .success(let alamofireUploadTask, _, let url):
                        alamofireUploadTask.suspend()
                        defer { alamofireUploadTask.cancel() }
                        if let alamofireUploadFileUrl = url {
                            var request = URLRequest(url: URL(string: "https://yourserver.com/photoUploadEndpoint")!)
                            request.httpMethod = "POST"
                            for (key, value) in alamofireUploadTask.request!.allHTTPHeaderFields! { // transfer headers from the request made by alamofire
                                request.addValue(value, forHTTPHeaderField: key)
                            }
                            // we want to own the multipart file to avoid alamofire deleting it when we tell it to cancel its task
                            // so copy file on alamofireUploadFileUrl to a file you control
                            // dispatch the request to the background session
                            // don't forget to delete the file when you're done uploading
                        } else {
                            // alamofire failed to encode the request file for some reason
                        }
                    case .failure:
                        // alamofire failed to encode the request file for some reason
                    }
            }
            )

不幸的是,这不适用于 Alamofire 5,有没有办法适应它?

【问题讨论】:

    标签: alamofire background-process urlsession nsurlsessionconfiguration


    【解决方案1】:

    Alamofire 5不支持后台URLSessionConfigurations,但是你贴的代码只是分段上传,还是支持就好了。

    您应该针对新版本的 Alamofire 重新评估有关上传的各种黑客行为。这真的没有任何意义。

    Alamofire 可以处理后台任务,这可以让您有时间完成上传。你可以阅读更多here

    【讨论】:

    • 这不是我的代码,我只是在寻找可以与 Alamofire 5 和后台上传一起使用的东西,这样我就不必重新复制多部分逻辑了。
    • 我添加了一个指向后台任务文档的链接,您可以使用该链接在后台在有限的时间内保持上传活动。没有办法在后台会话中使用 Alamofire。或者,您可以使用 Alamofire 的多部分编码并使用原始 URLSession 上传。
    • 什么意思?是否有执行此原始 URLSession 的示例代码?
    • @JonShier 您提供的网址是否有效?似乎与 TLS 客户端身份验证有关。您能否更新 URL 以更正一个?
    • @Jim 你是对的,固定!
    猜你喜欢
    • 2017-06-18
    • 2018-11-19
    • 2020-10-07
    • 2018-06-16
    • 2020-04-10
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2019-11-09
    相关资源
    最近更新 更多