On Tue, May 18, 2010 at 12:58 PM, Nick Zitzmann <n...@chronosnet.com> 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to