Re: How to associate a custom NSNotificationCenter with an NSRunLoop in a thread?
Re: How to associate a custom NSNotificationCenter with an NSRunLoop in a thread?
- Subject: Re: How to associate a custom NSNotificationCenter with an NSRunLoop in a thread?
- From: Greg Hurrell <email@hidden>
- Date: Tue, 28 Mar 2006 11:59:43 +0200
Yeah, that's right, that's what I expect. But still, I'm not getting
the notifications that I send... Any idea why?
For your information, the reason I want a NSNotificationCenter
subclass is because I am trying to follow Apple's suggestion from
"Notifications: Delivering Notifications To Particular
Threads" (http://gemma.apple.com/documentation/Cocoa/Conceptual/
Notifications/Tasks/Threading.html):
A better, but more complex, implementation would generalize the
behavior into either a subclass of NSNotificationCenter or a
separate class that would have one notification queue for each
thread and be able to deliver notifications to multiple observer
objects and methods.
I have a working solution based on Apple's sample code, but when I
try to follow their advice of making a better, more generalized
solution using an NSNotificationCenter subclass, I get no
notifications at all. In my initial test attempt I've just made an
empty subclass that adds no special behaviour, but when I try sending
notifications using it the thread's run loop never "sees" them. Swap
it out and use [NSNotificationCenter defaultCenter] and it works...
Cheers,
Greg
El 27/03/2006, a las 23:53, Greg Herlihy escribió:
A notification center is conceptually a singleton - it works best
because
all parties use the same one. And while you can create your own
notification
center - only your code will even know that it exists. So the only
kind of
notifications an object could ever expect to receive from this
center, are
those notifications that the application has sent to this center,
itself.
Greg
On 3/27/06 5:25 AM, "Greg Hurrell" <email@hidden> wrote:
In the worker thread of my multi-threaded app I find that I can
register for notifications from the default notification center (as
returned by [NSNotificationCenter defaultCenter]) and I receive those
notifications when I run the worker thread's run loop (as returned by
[NSRunLoop currentRunLoop]).
I am aware of the limitation that notifications will be received on
the thread from which they were posted. That's not the problem.
The problem is that if I try to use a different notification center,
for example, an NSNotificationCenter subclass, then I find that I
don't ever receive any notifications. Evidently the default
NSNotificationCenter has some kind of relationship with the current
NSRunLoop and it all just works automatically. But if you are using a
different center, you evidently need to set a relationship up between
it and the run loop, otherwise you'll never receive any notifications
when you run the run loop.
The question is, how? And what exactly needs to be done? My
inspection of by the NSNotificationCenter and NSRunLoop
documentation, as well as a class dump of those classes, doesn't
provide me any clues. I can see neither methods in
NSNotificationCenter for adding or registering run loops, nor methods
in NSRunLoop for adding or registering NSNotificationCenters. I can
only see methods for adding ports and timers to run loops, and there
are no methods in NSNotificationCenter for getting at any underlying
ports that it may (or may not) be using.
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden