b2cloud

14th March 2013

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

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

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.

 

pthread

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.

Recommended Posts

NSNotificationCenter for iOS and Mac

Post by 4 years ago

If you've ever needed to have an object with multiple delegates you may have created an array and then added each of your delegates to it. This does work but there's a simpler way which...

Got an idea?

We help entrepreneurs, organizations and established brands from around
the country bring ideas to life. We would love to hear from you!