PDA

View Full Version : NSNotificationCenter selector callback never made




siggivara
Aug 11, 2010, 03:26 PM
I have a app that logs into a website. I had problems because I checked if I was logged in before the asynchronous web request finished. So I "solved" this using notification to notify when the web request finished, and then continuing to check if login was successful.

The problem now is that the callback to the selector eventHandler is never made, so nothing is happening.

-(IBAction) login:(id)sender {
// Add notification to listen to
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(eventHandler)
name:@"requestFinished"
object:nil];
}

-(void)eventHandler:(NSNotification *) notification {
NSLog(@"LoginViewController: Request finished and event triggered");
}// Posting notification that the web request finished
[[NSNotificationCenter defaultCenter]
postNotificationName:@"requestFinished" object:nil];
Have I misunderstood the concept of notification or is there something else wrong?



chown33
Aug 11, 2010, 04:36 PM
The problem now is that the callback to the selector eventHandler is never made, so nothing is happening.

-(IBAction) login:(id)sender {
// Add notification to listen to
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(eventHandler)
name:@"requestFinished"
object:nil];
}

-(void)eventHandler:(NSNotification *) notification {
NSLog(@"LoginViewController: Request finished and event triggered");
}// Posting notification that the web request finished
[[NSNotificationCenter defaultCenter]
postNotificationName:@"requestFinished" object:nil];
Have I misunderstood the concept of notification or is there something else wrong?
The latter.

The red-hilited selector you've identified is not identical to the blue-hilited selector you've created.

You will need to use @selector(eventHandler:). The difference is subtle, a single colon, but extremely significant.

siggivara
Aug 11, 2010, 06:33 PM
The latter.

The red-hilited selector you've identified is not identical to the blue-hilited selector you've created.

You will need to use @selector(eventHandler:). The difference is subtle, a single colon, but extremely significant.
Sorry about that, it's a typo in the post. In my code I have it with the colon, but sadly it doesn't make a difference. The problem is still the same :(

chown33
Aug 11, 2010, 06:40 PM
Sorry about that, it's a typo in the post. In my code I have it with the colon, but sadly it doesn't make a difference. The problem is still the same :(

Post your actual code.

Post the code that sends the notification.

Add an NSLog() call to your login: method, to confirm it's being called. Or set a breakpoint on login:.

Is it running on the simulator or the device when it exhibits the problem? If it's only one, then try it on the other.

siggivara
Aug 11, 2010, 07:14 PM
Post the code that sends the notification.

Well this is kinda embarrassing. I put the postNotificationName in the wrong place :p I had it in the applicationDidFinishLaunching, whereas I was supposed to have it in the callback from the webRequest, and the whole time I thaught that was where I had put it. The way it was I posted the notification before I started listening for it.
Thank you for making me check my code, with the right perspective, it works like a charm now :)

chown33
Aug 11, 2010, 08:09 PM
Well this is kinda embarrassing. I put the postNotificationName in the wrong place :p I had it in the applicationDidFinishLaunching, whereas I was supposed to have it in the callback from the webRequest, and the whole time I thaught that was where I had put it. The way it was I posted the notification before I started listening for it.
Thank you for making me check my code, with the right perspective, it works like a charm now :)

No trouble. Been there often enough. In the process of putting together a cogent question, with code and explanation, I'd spot the problem and immediately see how to fix it.

It's a useful problem-solving technique because it makes you look at your code in a new way, with the eyes of someone seeing it for the first time. After a few like that, you learn to do it for yourself. Explain the problem and what you've tried to your future self, a year or two from now, after you've forgotten the details so you need a guided tour of the code.