How to execute alamofire background upload request?

前端 未结 3 1957
小鲜肉
小鲜肉 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)
        }
    }
    })
    
    0 讨论(0)
  • 2021-01-05 13:24

    did you see this section Open Radars:

    Open Radars

    The following radars have some effect on the current implementation of Alamofire.

    rdar://26870455 - Background URL Session Configurations do not work in the simulator

    0 讨论(0)
  • 2021-01-05 13:30

    from Background Transfer Considerations :

    Only upload tasks from a file are supported (uploading from data objects or a stream will fail after the program exits).

    that means it is limitation from NSURLSession - you need you upload from a file and then try to solve the other error with file

    Update

    appDeligate.log.debug("request was sended")
    
    let tempZipFilePath = UtilDirectory.tempZipPath.tweak()
    
    alamoFireManager.upload(tempZipFilePath,
                            to: deligate.url,
                            method: .post,
                            headers: headers)
    
    0 讨论(0)
提交回复
热议问题