Re: NSNotificationCenter problem using new blocks style API calls
Re: NSNotificationCenter problem using new blocks style API calls
- Subject: Re: NSNotificationCenter problem using new blocks style API calls
- From: Jeremy Gordon <email@hidden>
- Date: Tue, 6 Oct 2009 00:01:49 -0700
Yup, something basic. From the documentation:
"You must retain the returned value as long as you want the
registration to exist in the notification center."
Once I held on to the return value from addObserverForName to protect
it from garbage collection, everything worked as expected.
Nothing like solving your own problem in public :)
On Oct 5, 2009, at 11:30 PM, Jeremy Gordon wrote:
Hi,
I'm experiencing a problem with NSNotificationCenter when trying to
use the new blocks style interface.
If I register an observer to call me back the traditional way via a
selector, then all is well.
If I register an observer and pass it a block of code, the code in
my block is never executed (well at least I don't see any log
message, don't get any breakpoints, and if I put code with side
effects in the block, I don't see the side effects happen).
I have tried passing in both [NSOperationQueue mainQueue] for the
queue parameter as well as nil. Although the example code below
shows me registering observers using both the traditional and block
styles, I have tried this with registering either or of the two
styles.
I am using blocks fine in other places in my own code where I copy
the block passed to me and call it back when I've completed
something. FYI, I am using Clang/LLVM, garbage collection is
enabled and obviously I'm targeting 10.6 to be able to use the new
API.
Must be something basic I'm missing...right? :)
Thanks!
Jeremy
- (void)myNotification:(NSNotification*)notification
{
NSLog(@"notified the traditional way, this is printed to the log");
}
- (id)init
{
...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myNotification:)
name:@"myNotification"
object:myObject];
[[NSNotificationCenter defaultCenter]
addObserverForName:@"myNotification"
object:myObject
queue:nil
usingBlock:^(NSNotification* notification)
{
NSLog(@"should be notified inside of the block, but never happens
and nothing printed to the log");
}];
...
}
_______________________________________________
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