本文介绍了从后台返回后,Alamofire进度回调停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种将(视频)数据上传到服务器的方法,它看起来像这样:

I have a method that uploads (video) data to the server, and it looks like this:

 static func upload(video data:Data, named name:String, parameters:[String:Any],  toUrl url:URL, progress:@escaping (Double)->Void, completion:@escaping(Bool)->Void){
manager = Alamofire.SessionManager(configuration: URLSessionConfiguration.background("com.app.backgroundtransfer")
        manager.upload(multipartFormData: { (multipartFormData) in

            multipartFormData.append(data, withName: "filedata", fileName: name, mimeType: "video/quicktime")

            for key in parameters.keys{

                if let val = parameters[key] as? String{

                    multipartFormData.append(val.data(using: .utf8, allowLossyConversion: false)!, withName: key)
                }
            }

        }, to: url) {
            (encodingResult) in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.uploadProgress(closure: { (uploadProgress) in

                    progress(uploadProgress.fractionCompleted)
                    //this one stops getting called

                })
                upload.responseJSON { response in
                   // but this one gets called at the end.
                }
            case .failure(let encodingError):
              print(encodingError)
            }
        }
    }

问题是,从后台返回时(在上传中间),我无法正确更新UI.

So the problem is, I can't update UI properly when returned from background (while in the middle of upload).

为什么此进度回调停止工作(从后台返回)?

Why this progress callback stops working (after returning from background) ?

推荐答案

Alamofire与后台会话并不真正兼容.由于这是一个基于闭包的API,当应用程序进入后台时无法持久保存,因此,当应用程序前台运行时,不会关闭您的进度闭包.我们建议您直接使用URLSession或使用后台任务API代替后台会话.

Alamofire isn't really compatible with background sessions. Since it's a closure-based API that can't be persisted when the app goes into the background, your progress closures aren't being reconnected when the app is foregrounded. We recommend you use URLSession directly or use the background task API instead of background sessions.

这篇关于从后台返回后,Alamofire进度回调停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 17:18