AFNetworking - Cancelling operations
I recently ran into trouble trying to cancel operations in AFNetworking.
Two issues came up. The first was finding my previous query, to cancel it. The second was receiving some kind of notification that the operation was cancelled. The failure block isn’t fired when operations are cancelled.
Since my app uses GET requests, cancelAllHTTPOperationsWithMethod:path: was failing to find my operation. It was looking for a full path, query string and all. Since the query parameters are off somewhere else in my app, it was much easier to just override this method, and write code to match just the URL, ignoring the query string. Along the way, I improved on this StackOverflow question regarding trimming the query string off an NSURL.
The next bit required digging into the GitHub issues for AFNetworking to resolve. The default behaviour of AFNetworking is to call the failure block if the HTTP request fails, the success block if it succeeds, and do nothing if you cancel it. The logic is that if you are cancelling it, you can probably deal with the cancellation however you want. Details are available in AFNetworking/Issue#479. The workaround for this is to set the completionBlock property manually, instead of using setCompletionBlockWithSuccess:failure:.



