Question

I'm stuck canceling downloads with AFNetworking. In my app, the user can trigger single movie downloads this way:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
                                         {
                                             //do success stuff

                                         }
                                 failure:^(AFHTTPRequestOperation *operation, NSError *error)
                                         {
                                             NSLog(@"Error downloadMovie: %@", error);
                                         }];
[operation start];

That works fine. But how can I force a download to be stopped? I read about using this method:

cancelAllHTTPOperationsWithMethod

If I use it this way, it doesn't do anything:

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:@"http://www.xyz/mymovie.mp4"];
[client cancelAllHTTPOperationsWithMethod:nil path:@"http://www.xyz/mymovie.mp4"];

What would be the proper way to cancel the download?

Many thanks in advance.

Was it helpful?

Solution

AFHTTPRequestOperation is a subclass of NSOperation, which has a -cancel method. Use that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top