Swift Resume Alamofire upload after timeout/lost connection

烂漫一生 提交于 2019-12-10 13:17:57

问题


Im using swift 2.3 and Alamofire with multipartFormData to upload files asynchrononously and cant seem to figure out if it even is possible to make a resume after i get a timeout on my connection or lost my connection and it is reestablished on the phone.

What I want is to resume the upload if it fails, for example: I upload a large file. It gets to 55% uploaded, and then I lose my internet connection. When I regain the internet connection, I want the upload to continue from 55% instead of starting from 0% again.

Following is the code I use for uploading my files:

class FileUploadHelper
{
    class func Upload(numberOfTimes: Int, index: Int, command: String, nsData: NSData, fileName: String, mimeType: String, progress: (index: Int, progressPercentage: Int) -> Void, error: (index: Int, fileUploadError : FileUploadError) -> Void, completed: (index: Int, dataFail: Int) -> Void, badConnection: () -> Void)
    {
        Alamofire.upload(.POST, ResourceHelper.ServiceBaseUrl + command, multipartFormData: 
            { 
                multipartFormData in
                multipartFormData.appendBodyPart(data: nsData, name: "test", fileName: fileName, mimeType: mimeType)
            }, encodingCompletion:
            {
                encodingResult in 
                encodingCompleted(numberOfTimes, index: index, command: command, nsData: nsData, fileName: fileName, mimeType: mimeType, encodingResult: encodingResult, progress: progress, error: error, completed: completed, badConnection: badConnection)
            }
        )
    }

    class func encodingCompleted( numberOfTimes: Int, index: Int, command: String, nsData: NSData, fileName: String, mimeType: String , encodingResult: Manager.MultipartFormDataEncodingResult, progress: (index: Int, progressPercentage: Int) -> Void, error: (index: Int, fileUploadError : FileUploadError) -> Void, completed: (index: Int, dataFail: Int) -> Void, badConnection: () -> Void)
    {
        switch encodingResult {
            case .Success (let upload, _, _):
                upload.responseString {
                    response in
                    var out: NSInteger = 0
                    response.data!.getBytes(&out, length: sizeof(NSInteger))

                    switch response.result {
                        case .Success:
                            dispatch_async(dispatch_get_main_queue(), {
                                completed(index: index, dataFail: out)
                            })
                        case .Failure:
                            if (numberOfTimes > 0) {
                                AWBanner.showWithDuration(7, delay: 0, message: NSLocalizedString("Bad Connection - Attempting to upload", comment: ""), backgroundColor: UIColor.redColor(), textColor: UIColor.whiteColor(), originY: 40.0)
                                FileUploadHelper.Upload(numberOfTimes - 1, index: index, command: command, nsData: nsData, fileName: fileName,mimeType: mimeType, progress: progress, error: error, completed: completed, badConnection: badConnection)
                            }
                            else {
                                badConnection()
                            }
                    }
                }

                upload.progress
                {
                    bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
                    dispatch_async(dispatch_get_main_queue(), { 
                    progress(index: index, progressPercentage: Int(Double(totalBytesWritten) / Double(totalBytesExpectedToWrite) * 100))

                    })
                }
            case .Failure(let encodingError):
                dispatch_async(dispatch_get_main_queue(), {
                    error(index: index, fileUploadError: FileUploadError.Failed(encodingError: encodingError))      
                })
        }
    }
}

来源:https://stackoverflow.com/questions/41137536/swift-resume-alamofire-upload-after-timeout-lost-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!