koen
koen

Reputation: 5729

How do I cancel a download with AFHTTPSessionManager?

My app downloads information using a subclass of AFHTTPSessionManager and GET. The user may opt out of the download and I am trying to figure out how to cancel the download.

I came across this code:

- (void) cancel {
    [self.operationQueue cancelAllOperations];
}

But that didn't seem to work, since the download completed.

I then tried:

- (void) cancel {
    for (NSURLSessionTask *task in self.tasks) {
        [task cancel];
    }
}

This did seem to interrupt the download, but I am not sure if this is the proper way to do this. The docs are not very helpful for this (or I haven't found it yet).

So, is the above the correct way to cancel my download? Do I need to do more cleanup?

Upvotes: 1

Views: 2510

Answers (1)

Rob
Rob

Reputation: 437632

You certainly do not want to use cancelAllOperations. That is fine with operation-based AFHTTPRequestOperationManager requests, but not the task-based AFHTTPSessionManager requests. Sadly, AFHTTPSessionManager does not use operations. I understand why (wrapping the tasks in operations is surprisingly complicated given the fact that NSURLSessionTask delegates are specified at the session level (!), it's incompatible with background sessions, etc.), but it's too bad, as it significantly diminishes the value of AFHTTPSessionManager, IMHO. Mattt once said that he was going to remedy this, but it's never been released.

Your second approach of canceling the individual tasks is fine. You could also call invalidateSessionCancelingTasks, but you'd presumably have to re-instantiate the session (or the entire AFHTTPSessionManager) when you want to initiate more requests.

Upvotes: 4

Related Questions