Making a Window Class Invocable from Any Thread
Making a Window Class Invocable from Any Thread
- Subject: Making a Window Class Invocable from Any Thread
- From: Jerry Krinock <email@hidden>
- Date: Tue, 4 Nov 2008 14:22:35 -0800
I have a class which programatically creates and manipulates an alert
window, like NSAlert except it's capable of much more. It exposes
about 55 methods, doing stuff like like -setTitle:, -setButtonTitle:, -
setProgressAmount:, -removeAllSubviews, -setPopupTitles:,
setIconStyle: etc., etc.
Now that I'm learning the coolness of NSOperation, I'd like to be able
to invoke these methods from any thread (any NSOperation). In Apple's
NSOperationSample sample project, I see one instance of a simple
design pattern [1] which provides this any-thread invocability [2]:
The name of the method which directly manipulates the UI is prefixed
with "mainThread_", and then a second method is provided which simply
wraps this first method with an invocation of -
performSelectorOnMainThread:withObject:waitUntilDone:. The name of
the second method is prefixed with "anyThread_".
This seems like a good solution but before I write wrappers for 55
methods I was wondering if anyone knows a better approach to this
problem. (Of course, I'll be looking to consolidate some of my 55
methods.)
Thank you,
Jerry Krinock
[1] From NSOperationSample:
- (void)mainThread_handleLoadedImages:(NSNotification*)note
{
if ([myStopButton isEnabled])
{
[tableRecords addObject:[note userInfo]];
[myTableView reloadData];
// set the number of images found indicator string
[self updateCountIndicator];
}
}
//
------------------------------------------------------------------------
// This method is called from any possible thread (any NSOperation)
used
// to update our table view and its data source.
//
------------------------------------------------------------------------
- (void)anyThread_handleLoadedImages:(NSNotification*)note
{
// update our table view on the main thread
[self
performSelectorOnMainThread:@selector(mainThread_handleLoadedImages:)
withObject:note waitUntilDone:NO];
}
[2] Yeah, Mail's spellchecker didn't like 'invocability' but I think
it's cool.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden