Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFHTTPRequestOperation Cancel Request

Is there a way to cancel a AFHTTPRequestOperation? I am using it to download a PLIST for example. So I was planning to have a button to cancel the download or task. Is there a way?

Update:

operationQueue = [NSOperationQueue new];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:myURL] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFPropertyListResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id propertyList) {

    NSLog(@"DONE");        

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"%@", error);

}
 ];

 [operationQueue addOperation:operation];

So I have put "operation" to "operationQueue" and can stop the operation with:

[operationQueue cancelAllOperations];

This is only one operation in the queue. Is there a way to stop operation explicit if there are more than one? I guess the following error in the console - is it normal if a queue gets cancelled?

Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed.

like image 547
halloway4b Avatar asked Dec 16 '22 03:12

halloway4b


1 Answers

You can use:

[operation cancel];

This is an NSOperation method that's implemented by AFNetworking.

like image 146
Aaron Brager Avatar answered Jan 07 '23 17:01

Aaron Brager