How to associate a custom NSNotificationCenter with an NSRunLoop in a thread?
How to associate a custom NSNotificationCenter with an NSRunLoop in a thread?
- Subject: How to associate a custom NSNotificationCenter with an NSRunLoop in a thread?
- From: Greg Hurrell <email@hidden>
- Date: Mon, 27 Mar 2006 15:25:33 +0200
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.
Ideas, anyone? I imagine this is a fairly basic operation, but I
can't see how to do it.
The basic code I have looks like this:
// in detached thread...
NSNotificationCenterSubclass *center = [NSNotificationCenterSubclass
subclassCenter];
[center addObserver:self
selector:@selector(blah:)
name:blah
object:nil];
// add sources to run loop (ports, timers, whatever) then:
[[NSRunLoop currentRunLoop] run];
As above, I never get any notifications. But if I use
[NSNotificationCenter defaultCenter] I get notifications while
running the run loop...
Cheers,
Greg
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