Hemanth
Hemanth

Reputation: 143

Cancel AWSS3 TransferUtility Upload task on button tap

My requirement is to cancel upload when ever user wishes. I'm following TransferUtility document to setup and upload video, show progress, retains state even if app is in background. I couldn't find any way to cancel the uploadTask.

{
let uploadRequest = AWSS3TransferManagerUploadRequest()
let  transferUtility = AWSS3TransferUtility.default()
var completionHandler : AWSS3TransferUtilityUploadCompletionHandlerBlock?
var uploadTask : AWSTask<AWSS3TransferUtilityUploadTask>?
let expression = AWSS3TransferUtilityUploadExpression()

}


func startUpload() {

uploadTask = transferUtility.uploadFile((uploadRequest?.body)!,
                                   bucket: (uploadRequest?.bucket)!,
                                   key: (uploadRequest?.key)!,
                                   contentType: "video/mov",
                                   expression: expression,
                                   completionHander: completionHandler).continue({ (task) -> AnyObject! in if let error = task.error {
                                    print("Error: \(error.localizedDescription)")
                                    }
                                    if let _ = task.result {

                                    }
                                    return nil;
                                   }) as? AWSTask<AWSS3TransferUtilityUploadTask>


}

I want to know which continue block i should use on the task and how to perform something like uploadTask.cancel() just the way we use for TransferManager task.

Upvotes: 4

Views: 2802

Answers (2)

marcelosalloum
marcelosalloum

Reputation: 3531

I use theese two helper methods when I want to cancel one specific upload request or all upload requests:

static func cancelAllUploads() {
    AWSS3TransferUtility.default().enumerateToAssignBlocks(forUploadTask: { (uploadTask, progress, error) in
        uploadTask.cancel()
    }, downloadTask: nil)
}

static func cancelAllUploads(withTaskId taskId: UInt) {
    AWSS3TransferUtility.default().enumerateToAssignBlocks(forUploadTask: { (uploadTask, progress, error) in
        if uploadTask.taskIdentifier == taskId {
            uploadTask.cancel()
        }
    }, downloadTask: nil)
}

Upvotes: 3

Oleg Gordiichuk
Oleg Gordiichuk

Reputation: 15512

From the AWS documentation you should use AWSS3TransferManagerUploadRequest as point to cancel upload request.

So simply:

uploadRequest.cancel()

If you would like to cancel AWSS3TransferUtility task. You should work with them directly.

Use method enumerateToAssignBlocks to fetch all active AWSS3TransferUtility tasks. Or directly by using method getUploadTasks().

AWSS3TransferUtility.default().getUploadTasks()

After you get all active upload tasks simply use taskIdentifier of each task to find what task it is. And yes you should store taskIdentifier of task that you would like to cancel before.

Example of the cancel method.

static func cancel(taskIdentifier: UInt) {
        _transferUtility.enumerateToAssignBlocks(forUploadTask: { (uploadTask:AWSS3TransferUtilityUploadTask, progress:AutoreleasingUnsafeMutablePointer<(@convention(block) (AWSS3TransferUtilityTask, Progress) -> Void)?>?, error: AutoreleasingUnsafeMutablePointer<(@convention(block) (AWSS3TransferUtilityUploadTask, Error?) -> Void)?>?) in
            if uploadTask.taskIdentifier == taskIdentifier {
                uploadTask.cancel()
            }
        }, downloadTask: nil)
 }

Upvotes: 5

Related Questions