Best multithreading approach in Objective C? Best multithreading approach in Objective C? multithreading multithreading

Best multithreading approach in Objective C?


NSOperationQueue should work well for this task.

Another option would be plain GCD, however, if you've never worked with it, NSOperationQueue is probably the better choice since it pretty much automatically guides you to implementing things "the right way", has obvious ways for cancellation, etc.


You want to use Concurrent NSOperations to download and process images in the background. These would be managed by an NSOperationsQueue. Essentially these operations would be configured to fetch one image per operation, process it, save it in the file system, then message back to the main app in the main thread that the image is available.

There are several projects on github that you can look at that show how to do this - just search github using "Concurrent" or "NSOperation".

iOS has a really nice facility for doing background work. Grand Central Dispatch (GCD) and Blocks, but those don't let you have an object using delegate callbacks - thus NSOperation.

So you need to read up on blocks, GCD, and then look at some open source Concurrent NSOperations code. Using Concurrent NSOperations is not as simple as using blocks.


If I had this problem, I would probably go for an approach like this:

  • a single thread that will load the images, and causes the main thread to display results (I'm not a big fan of having thread mess around with GUI objects)

  • when a new directory is requested... well, it depends on how you want to manage things. Basically, a standard queue construct (condition variable and array) could be used for the main thread to tell the thread that "this directory will be needed" by passing it the path name; the thread will check the queue even when it's loading images (like after every image or so), and switch to the new directory whenever one shows up

  • you could make a directory-reader object that keeps all the state, and store this indexed by the path into a dictionary. When a new directory is requested, check that dictionary first, and only create a new object if there's none for this directory. That way, partially loaded directories would stick around until they are needed again, and can continue to load instead of having to start from scratch.

Pseudocode for the thread:

while (forever)   new element = nil   if we have an active directory loader       tell directory loader to load one image       if false then make directory loader inactive       lock queue condition       if queue has elements          new element = retrieve LAST element (we aren't interested in the others)          empty queue          unlock with status "empty"       else          unlock queue   else       lock queue on condition "has elements"       new element = retrieve last element       empty queue       unlock with status "empty"   if new element != nil       if directory loader for new path does not exist          setup new directory loader for new path          store in dictionary          make it the "active" one       else          make the current one the "active"

As for the directory loader, it might look something like this:

read one image:   if there are still images to read:       read, process and store one       return true   else       performSelectorOnMainThread with an "update GUI" method and the image list as parameter       return false;

This is just a quick sketch; there's some code duplication in the thread, and the way I wrote it will only update the GUI after all images have been read, instead of making them appear as we read them. You'll have to copy the current image list, or add synchronization if you want to do that.