簡體   English   中英

從后台返回后,Alamofire進度回調停止工作

[英]Alamofire progress callback stops working after returning from background

我有一種將(視頻)數據上傳到服務器的方法,它看起來像這樣:

 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。

為什么此進度回調停止工作(從后台返回后)?

Alamofire與后台會話並不完全兼容。 由於這是一個基於閉包的API,當應用程序進入后台時無法持久保存,因此當應用程序前台運行時,不會關閉您的進度閉包。 我們建議您直接使用URLSession或使用后台任務API代替后台會話。

因此,我所做的是使用后台任務(在沒有Alamofire的情況下也完全完成了此任務)並且有效。 像這樣:

 var backgroundTask: UIBackgroundTaskIdentifier = .invalid

    func registerBackgroundTask() {
        backgroundTask = UIApplication.shared.beginBackgroundTask {
            self.endBackgroundTask()
        }
        assert(backgroundTask != .invalid)
    }

    func endBackgroundTask() {
        print("Background task ended.")
        UIApplication.shared.endBackgroundTask(backgroundTask)
        backgroundTask = .invalid
    }

 APIClient.manager = Alamofire.SessionManager(configuration: URLSessionConfiguration.background(withIdentifier: "com.app.backgroundtransfer"))
            self.registerBackgroundTask()

            APIClient.upload(video: video, named: videoDetailsModel.videoURL!.lastPathComponent, parameters: self.uploadParameters, toUrl: url, progress: {[weak self] (percentage) in
                guard let `self` = self else {return}

                print("Progress \(percentage)")
                self.progressBar.progress = CGFloat(percentage)
                }, completion: { [weak self] (success) in
                    guard let `self` = self else {return}
                    if(success){
                        print("Video successfully uploaded")
                        self.endBackgroundTask()
                        self.progressBar.progress = 1.0
                    }else{
                        print("Video upload was not successfull")
                    }
            })

現在進度回調正常工作,您仍然可以使用Alamofire打包多部分數據,就像我正在使用它一樣,或者出於任何原因使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM