Anyone who’s ever had to perform a message on the main thread knows how ugly the code can be:
[obj performSelectorOnMainThread:@selector(receiveAnswer:) withObject:[NSNumber numberWithUnsignedInteger:42] waitUntilDone:NO];
I’ve written a category that changes the above code to something much simpler:
[[obj performOnMainThread_PRH] receiveAnswer:42];
Notice that not only is the code much easier to read and to write, but you no longer need to box and unbox primitive values.
You can get the code, under a BSD license, from the higher-order main-thread perform repository.
Back when I originally mentioned my creation of the category on Twitter, Dave Dribin replied that he had already written one of his own. His is part of his DDFoundation framework, and requires DDInvocationGrabber (a fork of Jonathan Wight‘s CInvocationGrabber), also part of that framework.
My category requires nothing but Cocoa and is not part of a larger framework. That said, it’s interesting to read in his code how the functionality of the Invocation Grabber makes his implementation nothing but a thin wrapper around it. (Also, his version lets you say