Alamofire multipartFormData响应时间超过1分钟后重新开始上传

时间:2019-03-02 05:39:33

标签: swift alamofire multipartform-data

我做了什么?

我正在尝试使用multipartFormData上传视频文件,并将timeoutIntervalForRequest设置为300, 超时为300的原因是,在上传视频后,我的服务器大约需要2分钟来进行编码和响应

我期望发生什么?

当服务器完成编码并以JSON数据响应时,我期望成功响应。

发生了什么事?

只要响应时间超过1分钟。 Alamofire重新开始重新上传,而不是等待响应或显示JSON响应。

  

PS:仅当我使用后台SessionManager时,才会出现此问题

Alamofire环境

** Alamofire版本:(4.8.0) ** Xcode版本: 10 **快速版本: 4.2 **运行Alamofire的平台: iOS 12

演示项目

func uploadInBackground(fileInData: Data) {        
        let headers: [String : String] = [ "Authorization": "key"]

        Networking.sharedInstance.backgroundSessionManager.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(fileInData, withName: "file", mimeType: "video/mp4")

        }, usingThreshold: UInt64.init(), to: url, method: .post, headers: headers)
        { (result) in
            switch result {
            case .success(let upload, _, _):

                upload.uploadProgress(closure: { (progress) in
                    //Print progress
                    let value =  Int(progress.fractionCompleted * 100)
                    print("\(value) %")
                })

                upload.responseJSON { response in
                    //print response.result
                    print(response.description)
                    let res = response.response?.statusCode
                    print(res)
                }

            case .failure(let encodingError):
                //print encodingError.description
                print(encodingError.localizedDescription)
            }
        }
    }.
class Networking {
    static let sharedInstance = Networking()
    public var sessionManager: Alamofire.SessionManager // most of your web service clients will call through sessionManager
    public var backgroundSessionManager: Alamofire.SessionManager // your web services you intend to keep running when the system backgrounds your app will use this
    private init() {
        let defaultConfig = URLSessionConfiguration.default
        defaultConfig.timeoutIntervalForRequest = 300


        let backgroundConfig = URLSessionConfiguration.background(withIdentifier: "com.test.app")
        backgroundConfig.timeoutIntervalForRequest = 300

        self.sessionManager = Alamofire.SessionManager(configuration: defaultConfig)
        self.backgroundSessionManager = Alamofire.SessionManager(configuration: backgroundConfig)

    }
}

0 个答案:

没有答案