var ebf: Blog // by Eduardo Fonseca

Thoughts on the Completion Block Pattern

Nowadays, most Open Source iOS libraries adopt the “Completion Block Pattern” that Apple introduced with iOS 4. That’s great, and I’m all for consistency. The problem is that most authors are not being careful or clear on which queue will the completion block be dispatched to.

Some assume that you must want it to be dispatched to the main queue. Others, just run it on whatever queue their code ran before. Heck, not even Apple is consistent.

I propose the following:

  • Allow the caller to specify a queue to dispatch the block;
  • Make it very clear on your method signature which queue the completion block will be dispatched to, e.g., doSomethingOnTheBackgroundWithThis:withMainQueueCompletionBlock:;

Sometimes, less is more. On this case, be verbose! Making the wrong assumption can be the source (of a lot of) unnecessary work.