Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61880

How to pause NSURLSessionDownloadTask?

This is how I use NSURLSessionDownloadTask:

    let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
    let sessionManager = AFURLSessionManager(sessionConfiguration: sessionConfiguration)

    if let url = NSURL(string: URLString) {

        let request = NSURLRequest(URL: url)
        let sessionDownloadTask = sessionManager.downloadTaskWithRequest(request, progress: nil, destination: { (url, response) -> NSURL in

            self.createDirectoryAtPath(destinationPath)

            return destinationPath.URLByAppendingPathComponent(fileName)

            }, completionHandler: { response, url, error in

                completionBlock(error)
        })

        sessionDownloadTask.resume()
    }

I know that I can call sessionDownloadTask.cancel() and sessionDownloadTask.resume(), but I need to call there .pause(), and then .resume() to continue download. Is it possible?

Upvotes: 3

Views: 2523

Answers (2)

Leo
Leo

Reputation: 24714

This method of NSURLSessionTask

.suspend()

Doc

A task, while suspended, produces no network traffic and is not subject to timeouts. A download task can continue transferring data at a later time. All other tasks must start over when resumed.

Upvotes: 4

Vinay Jain
Vinay Jain

Reputation: 1661

Don't call the sessionDownloadTask.cancel() instead use the call sessionDownloadTask.cancelByProducingResumeData and whenever you want to resume downloading use the method of NSURLSession

sessionManager.downloadTaskWithResumeData:resumeDataObject

Upvotes: 4

Related Questions