从后台返回后,Alamofire进度回调停止工作

编程入门 行业动态 更新时间:2024-10-03 21:23:47
本文介绍了从后台返回后,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进度回调停止工作

本文发布于:2023-11-26 23:18:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635618.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   进度   后台   工作   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!