• 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
Re: Notifications on main thread
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Notifications on main thread


  • Subject: Re: Notifications on main thread
  • From: Michael Ash <email@hidden>
  • Date: Tue, 18 May 2010 13:27:49 -0400

On Tue, May 18, 2010 at 12:58 PM, Nick Zitzmann <email@hidden> wrote:
>
> On May 18, 2010, at 8:34 AM, Jonny Taylor wrote:
>
>> - I could try acquiring the NSNotificationQueue for the main thread, but there does not appear to be a standard way of doing that. I have seen this suggested as a strategy elsewhere, but I think I have also read that one is not meant to post to queues other than that of the current thread (not sure why...).
> [...]
>> I feel there must be a simple way of doing what I want - can anybody advise?
>
> There is a way to do this. You need to set up the enqueue message in an NSInvocation, have it retain the arguments, and then call the invocation's -performSelectorOnMainThread:... method to invoke the invocation on the main thread. This works any time you need to call something on the main thread, but you need to set more than one argument, or the method has arguments that take primitives instead of objects.

Note that if you're targeting 10.6 then you can do it MUCH more easily
by using blocks and either NSOperationQueue or GCD:

// NSOpQ
[[NSOperationQueue mainQueue] addOperationWithBlock: ^{ /* post your
notification here */ }];

// GCD
dispatch_async(dispatch_get_main_queue(), ^{ /* post your notification
here */ });

And if you need it, you can also use dispatch_sync in place of
dispatch_async in order to have the calling code block until the
notification has finished posting. This is also possible with
NSOperationQueue, but requires substantially more code, so the GCD
route is probably better. (You should try to avoid sync operations
when multithreading when possible, but sometimes you need it.)

Mike
_______________________________________________

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: Notifications on main thread
      • From: Jonny Taylor <email@hidden>
References: 
 >Notifications on main thread (From: Jonny Taylor <email@hidden>)
 >Re: Notifications on main thread (From: Nick Zitzmann <email@hidden>)

  • Prev by Date: Re: My program causes MacBook Pro to use NVidia graphics processor
  • Next by Date: Re: (solved?) baseURL problem with +fileURLWithPath:
  • Previous by thread: Re: Notifications on main thread
  • Next by thread: Re: Notifications on main thread
  • Index(es):
    • Date
    • Thread