Taking (some) Pain out of Multithreading
v0.7.1 Only makes a change to the example project for the sake of Xcode 3.2.
ThreadWorker is a class whose sole class-level method makes it easy to offload a task to another thread and notify your application when you've completed the task. Inspriation comes from Java's SwingWorker class. Creating Multithreaded applications with Objective-C in Mac OS X's Cocoa framework is easier than raw C code, but there are still some difficulties that ThreadWorker will help you overcome.
With the advent of libdispatch (Grand Central Dispatch) and blocks in Mac OS X 10.6 Snow Leopard, this style of multithreading may be on its way out, but in the meantime, I still find it handy!
Follow the file release RSS feed...
Be sure to copy the ThreadWorker.h and ThreadWorker.m files to your project directory.
Here is an example of how you would call ThreadWorker from your class. There is more information on the API page.
[ThreadWorker workOn:self withSelector:@selector(longTask:) withObject:userInfo didEndSelector:@selector(longTaskFinished:)];
The longTask method in self will then be called and should look like this:
- (id)longTask:(id)someData { // Do something that takes a while and uses 'someData' if you want // ... return userInfo; // Will be passed to didEndSelector }
Or view ThreadWorker.h or ThreadWorker.m directly.
Enjoy!
- 0.7
- Added ability to mark thread as cancelled.
- Changed the behavior when "longTask" takes a second argument. Instead of passing a proxy to the primary thread's "self" it passes a references to the ThreadWorker. The recommended way to pass information from the primary, or originating, thread is to use an NSDictionary to pass in the Things You'll Need. See the Controller.m example.
- Changed thread's termination behavior so that as soon as your "longTask:" is finished, the thread will exit. This means if you left anything on the NSRunLoop (or more likely an NSURL did it without your knowledge), it will get dumped.
- v0.6.2 - Moved [super dealloc] to end of dealloc and ensured "init" returns nil if [super init] fails.
- v0.6.1 - Added [super dealloc] to the dealloc method and moved the dealloc declaration out of the private API and into the public .h file as it should be.
- v0.6 - Eliminated the need for the runSelectorInCallingThread method by making a proxy to the target available to the task working in the new thread. This makes for much less overhead. Also changed static method signature from withArgument to withObject.
- v0.5.1 - Oops. Forget an NSConditionLock needed in the creation of the NSConnection.
- v0.5 - Uses NSConnection to communicate between threads, so although we might have been thread-safe before (depending on whether or not addTimer in NSRunLoop is thread-safe), we're definitely thread-safe now - or so we think. =) In the process we had to do away with the helper functions that took a bit of hassle out using runSelectorInCallingThread with arguments that are not objects. Sorry.
- v0.4.1 fixes some typos in the documentation.
- New in version 0.4: Now in the Public Domain!
- New in version 0.3: Now provides methods for making calls to the main calling thread from within your secondary thread!
A Note About Public Domain
I have released this software into the Public Domain. That means you can do whatever you want with it. Really. You don't have to match it up with any other open source license — just use it. You can rename the files, do whatever you want. If your lawyers say you have to have a license, contact me, and I'll make a special release to you under whatever reasonable license you desire: MIT, BSD, GPL, whatever.