Im using swift 2.3 and Alamofire with multipartFormData to download files asynchronously and can not seem to figure out whether it is even possible to resume after I get a timeout in my connection or lost my connection and it is restored by phone.
I want to resume downloading if this fails, for example: I am downloading a large file. It loads up to 55%, and then I lose my internet connection. When I get back to the internet connection, I want the download to continue from 55% instead of starting again from 0%.
Below is the code I use to upload 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))
})
}
}
}