How to execute alamofire background upload request?

前端 未结 3 1958
小鲜肉
小鲜肉 2021-01-05 12:39

I need to send zip file to server side.

There is my request which I need to work in background

let configuration = URLSessionConfiguration.default
             


        
3条回答
  •  攒了一身酷
    2021-01-05 13:16

    Use below code once, Its working for me

    import Alamofire

    var sessionManager: Alamofire.SessionManager
    var backgroundSessionManager: Alamofire.SessionManager 
    self.sessionManager = Alamofire.SessionManager(configuration: URLSessionConfiguration.default)
    self.backgroundSessionManager = Alamofire.SessionManager(configuration: URLSessionConfiguration.background(withIdentifier: "com.youApp.identifier.backgroundtransfer"))
    
    
    backgroundSessionManager.upload(multipartFormData: blockFormData!, usingThreshold: UInt64.init(), to: url, method: .post, headers: APIManager.headers(), encodingCompletion: { encodingResult in
    switch encodingResult {
    case .success(let upload, _, _):
        upload.uploadProgress {
            (progress) in
            let p = progress.fractionCompleted * 100
            uploadProgress(p)
        }
        upload.responseJSON { response in
            switch(response.result) {
            case .success(let JSON):
                DispatchQueue.main.async {
                        print(JSON)
                }
            case .failure(let error):
                DispatchQueue.main.async {
                    print(error)
                }
            }
        }
    case .failure(let error):
        DispatchQueue.main.async {
            print(error)
        }
    }
    })
    

提交回复
热议问题