Re: Making a Window Class Invocable from Any Thread
Re: Making a Window Class Invocable from Any Thread
- Subject: Re: Making a Window Class Invocable from Any Thread
- From: Charles Steinman <email@hidden>
- Date: Tue, 4 Nov 2008 15:10:25 -0800 (PST)
This sounds like it might be a good case for a proxy. You could create a small class that looks something like this:
@implementation MyClass
@synthesize targetWindow;
- (void)forwardInvocation:(NSInvocation *)invocation {
[invocation performSelectorOnMainThread:@selector(invokeWithTarget:) withObject:self.targetWindow waitUntilDone:NO];
}
@end
Not as efficient, so it wouldn't be good if these methods are being called a lot in a small period of time, but it's a lot less tedious than writing 50 dummy methods.
Cheers,
Chuck
----- Original Message ----
> From: Jerry Krinock <email@hidden>
> To: Cocoa Developers <email@hidden>
> Sent: Tuesday, November 4, 2008 2:22:35 PM
> Subject: Making a Window Class Invocable from Any Thread
>
> 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
_______________________________________________
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