AFNetworking/AFNetworking — pull request #202 by steipete (2012-02-06 21:53:07+00:00)
Title: Custom callback queues for the completionBlock

This is the first draft of the changes I'd like to see in AFNetworking to support custom callback queues, as already discussed with @mattt, @samsoffes and @followben.

I've seen that others already picked up the same idea (e.g. https://github.com/seatme/AFNetworking/blob/master/AFNetworking/AFHTTPRequestOperation.m), but my approach is a little different.

As for my usage, I am only interested in processing data on a *success* event in background, thus there now are two more properties in AFHTTPRequestOperation: successCallbackQueue and failureCallbackQueue. This removes the need to encapsulate the failure case with a dispatch to main, and probably fixes some future bugs by developers who would have missed that. The properties are NULL per default, and the main queue will be used, as it is currently. If they are set, they retain the dispatch queue and use this one instead.

I also added a "internal" category to streamline the calls to failure/success blocks, removing some duplication code and also fixing a potential bug in AFXMLRequestOperation (there was a dispatch_async missing)

Gotchas: There might be a race condition, if the callbackQueue variables are changed while the callbackBlock is called. We could add some basic locking for this, however I'm not sure how other cases like this are handled in AFNetworking.