Which tasks are more suitable to NSOperation than GCD?

dubbeat picture dubbeat · Dec 3, 2010 · Viewed 9.1k times · Source

Which tasks would be better suited to using NSOperation as opposed to using GCD when programming for the iPhone?

To me they seem to do the same thing. I can't see the strengths and weaknesses one has over the other.

Answer

Yuji picture Yuji · Dec 3, 2010

NSOperation is built on top of GCD, so the question is more about whether you use NSOperation or pass a block directly to GCD.

An NSOperation is bulky and needs more boiler-plate codes to set it up, but it has a lot more functionality. You can create the same NSOperation subclass in various parts of your code and put them into the queue and run it.

Passing a block to GCD by e.g. dispatch_async is quick and disposable. You typically don't reuse a block anywhere else; you just set up a block which is executed only at that point of the code, passes it to the GCD or other APIs, and quickly go on.

So each has its merits.