• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Making a Window Class Invocable from Any Thread
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Making a Window Class Invocable from Any Thread
      • From: Chris Hanson <email@hidden>
    • Re: Making a Window Class Invocable from Any Thread
      • From: Nick Zitzmann <email@hidden>
  • Prev by Date: Re: Programatically Change NSMenu Title
  • Next by Date: Re: Making a Window Class Invocable from Any Thread
  • Previous by thread: Re: 64-bit swizzling
  • Next by thread: Re: Making a Window Class Invocable from Any Thread
  • Index(es):
    • Date
    • Thread