4 ways to thread (iOS and Mac)

Guides | Tutorial By 4 years ago

There are many ways to thread on iOS and Mac, each have their own advantages and disadvantages.


NSOperation is probably the preferred method of threading. An NSOperation is added to an NSOperationQueue which will run through each of the operations on a thread. Each NSOperation can be cancelled quite easily (of course your code will need to check whether the work is cancelled every so often to end as soon as it can). NSOperation also makes it very easy to update the main thread once the operation is complete.


Grand Central Dispatch

GCD is what NSOperation is built upon. If you just want to do something extremely simple that an NSOperation might not be required for, then GCD can be a good option.



NSThread will detach a thread to a method. I don’t use NSThread very often, I find the others are a lot more powerful and easier to keep track of. Be aware that the target an NSThread is detached to will be retained until the thread ends.



The three examples above are all iOS and Mac exclusive (Foundation.framework). If you are building something multiplatform then obviously you need a multiplatform threading solution. pthread is one option, but it is very low level and raw.