Search code examples
iosasihttprequest

ASIHTTPRequest - best practice to cancel multiple request?


I've got a for-loop which sends multiple ASIHTTPRequests:

for (int i = 0; i < 10; i++) {
     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
     [request setDelegate:self]; 
     [request setDidFinishSelector:@selector(requestDone:)];
     [request setDidFailSelector:@selector(requestWentWrong:)];
}

To safely handle the delegate being deallocated before the requests have finished, I'd like to cancel those requests; how can I achieve that? Store all the requests in a NSMutableArray? Or can I achieve that using a queue? Thanks a lot for your help!

Stefan


Solution

  • The best solution depends on your architecture and application needs - eg. Do you have multiple http requests only some of which you might want to cancel?

    Here's one solution - it'll cancel and remove the delegate of all in progress requests that have been assigned to the default queue:

    for (ASIHTTPRequest *req in ASIHTTPRequest.sharedQueue.operations)
    {
        [req cancel];
        [req setDelegate:nil];
    }
    [queue setDelegate:nil];