Re: Notifications on main thread
Re: Notifications on main thread
- Subject: Re: Notifications on main thread
- From: Jonny Taylor <email@hidden>
- Date: Wed, 19 May 2010 12:02:13 +0100
>>> - 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 */ }];
Thankyou all three for your very helpful responses. The blocks-based approach is particularly nice. I haven't been able to do much with blocks up to now, because bad things happen as soon as they catch sight of a hint C++ code.
I've noticed a slight issue (with both NSInvocation and NSOpQ) when a menu is pulled down and posting styles NSPostASAP or NSPostWhenIdle are used. While the menu is visible, the notifications are posted ok (on the main thread), but objects do not receive the notifications until the menu is released. I don't quite understand why this should be (is the main loop not running when a menu is displayed, or are menu-related events somehow jumping the queue and preventing my notifications from reaching the head of the queue?). I feel there must be a way of keeping the GUI updated while the menu is down - any suggestions?
Many thanks
Jonny_______________________________________________
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