Interested in Leopard / Mac OS 10.5?
Thursday, January 25th, 2007It might pay off to search the internet for NSOperationQueue…
It might pay off to search the internet for NSOperationQueue…
For Kompressor.app, I’ve written a group of Foundation classes for implementing a worker-thread paradigm in Objective-C. The general idea is you have some non-trivial amount of processing you want done and then (optionally) be notified when it’s done.
This implementation has a number of advantages compared to others:
How do you use it?
If you want to wait for a batch of N work units to be done, create an NSConditionLock L, and at the end of the work selector, you do [L lock]; [L unlockWithCondition:[L condition] + 1];. After dispatching all the work units in the main thread (or wherever), you wait with [L lockWhenCondition:N]; [L unlockWithCondition:0];.
An (untested as I am only on a Linux machine at the moment) example demonstrating both simple usage as well as waiting for a batch to be finished is in BatchExample.m.
Remember to only access shared data structures (or your user-interface) in the didEndSelector. SIWorkManager also has messages for removing work on or referring to a given object. Use nil to remove all pending work units.
Here is the source code with all required files. Feedback / fixes appreciated.
While coding during the last month I’ve been listening to a lot of music via Pandora (a music recommendation service / internet radio station). I’m listing some of the stand-out tracks / artists I’ve enjoyed here