应用程序处于后台时,Alamofire后台上载不会打印进度

时间:2022-06-15 01:28:40

I am uploading PDF file to server using Alamofire. I have created backgroundSessionManager to make sure that app uploads file while app is in background. The code works perfectly as I am getting call over backgroundCompletionHandler from AppDelegate method handleEventsForBackgroundURLSession.

我正在使用Alamofire将PDF文件上传到服务器。我创建了backgroundSessionManager以确保应用程序在后台运行时上传文件。当我从AppDelegate方法handleEventsForBackgroundURLSession调用backgroundCompletionHandler时,代码完美地运行。

The progress is I am showing progress and when app goes in background the progress is stuck and .uploadProgress completion is also not calling. Due to this when user goes in background and after some time when comes back then progress is not increasing.

进度是我正在显示进度,当应用程序进入后台时,进度卡住了.uploadProgress完成也没有调用。因此,当用户进入后台并且在返回一段时间之后,进度不会增加。

Shared Instance:

共享实例:

class Networking {
    static let sharedInstance = Networking()
    public var backgroundSessionManager: Alamofire.SessionManager // your web services you intend to keep running when the system backgrounds your app will use this
    private init() {
        self.backgroundSessionManager = Alamofire.SessionManager(configuration: URLSessionConfiguration.background(withIdentifier: "com.dw.myapp"))

        var backgroundCompletionHandler: (() -> Void)? {
            get {
                return backgroundSessionManager.backgroundCompletionHandler
            }
            set {
                backgroundSessionManager.backgroundCompletionHandler = newValue
            }
        }
    }
}

Uploading file code

上传文件代码

Networking.sharedInstance.backgroundSessionManager.upload(multipartFormData: { (multipartData) in
 }, usingThreshold: UInt64.init(), to: "\(url)", method: .post, headers: headers) { (result) in
            switch result {
            case .success(let upload, _, _):
                upload.uploadProgress(closure: { (progress) in
                //Does not call while app is in background
                        Helper.dispatchMain {
                            print(progress)
                            print("Upload Time ", Date())
                        }
                    })
                    upload.responseJSON { response in
                        print("Response Time ", Date())
                        print("Response Time ", Date())
                    }
                case .failure(let error):
                    completion!(["message" : error],false)
                }
            }

I have search over SO and github page but din't find the solution. Any one can pls guide if I am making any mistake?

我搜索了SO和github页面但是没有找到解决方案。如果我犯了任何错误,任何人都可以指导吗?

1 个解决方案

#1


0  

应用程序处于后台时,Alamofire后台上载不会打印进度

Hope this will work, let me know if you are still facing it

希望这会有效,如果您仍然面对它,请告诉我

#1


0  

应用程序处于后台时,Alamofire后台上载不会打印进度

Hope this will work, let me know if you are still facing it

希望这会有效,如果您仍然面对它,请告诉我