-1

i am using following code

First.m:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerTok) name:@"registerTok" object:nil];  

[Second serviceCall:[NSString stringWithFormat:@"%@%@",BASEURL, USER_LOGIN] withParameter:parameters ofType:USER_SIGNIN];

Second.m:

+(void)serviceCall:(NSString*)url withParameter:(NSDictionary*)parameter ofType:(int)type{

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:url]];  
        --------------line1------------------  
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];  
        manager.responseSerializer = [AFJSONResponseSerializer serializer];  
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        [manager POST:url parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject)  {

            [[NSNotificationCenter defaultCenter] postNotificationName:@"registerToken" object:nil userInfo:nil];---breaking101

}

The notification code above is breaking. If I write [[NSNotificationCenter defaultCenter] postNotificationName:@"registerToken" object:nil userInfo:nil]; at line1, it is working. I think it is related to some object issue. Please help. I have never used notification center.

Akshara
  • 141
  • 1
  • 11

3 Answers3

2

There are a few things wrong here:

  1. The notification that you are sending ("registerToken") is not the one you are listening ("registerTok").
  2. The selector that you define should take one parameter - (NSNotification *), as mentioned in the apple doc here
  3. The success block of the POST:parameters:success: method of the AFHTTPRequestOperationManager is executed on arbitrary thread. You might want to specify the thread/queue on which you want to execute the method on when the notification is fired. When you post the notification on line 1 it works because that notification is executed on the current thread.

P.S. If you still have problems, add complete logs of the errors that you are getting, then one can better answer your question.

paresh
  • 1,174
  • 9
  • 13
  • I didnot understand this crash log: Here is the crash May 24 16:19:49 Aksharas-iPhone wifid[15] : WiFi:[485779789.610131]: May 24 16:19:49 Aksharas-iPhone wifid[15] : Too frequent(0.283584 secs) rssi event from driver, ignoring May 24 16:19:49 Aksharas-iPhone wifid[15] : May 24 16:19:50 Aksharas-iPhone lockdownd[52] : 00281000 set_response_error: handle_get_value GetProhibited – Akshara May 24 '16 at 10:52
  • Is this the exact line of crash? The last line where your program execution stops? – paresh May 24 '16 at 10:57
  • Yes.. I had added a string "Here is the crash" just before the line that is crashing. It is giving EXC_BAD_ACCESS – Akshara May 24 '16 at 11:02
  • The error that you posted is not related to your `NSNotification` usage. Here is the explanation for it: http://stackoverflow.com/questions/22460991/ios-too-frequent-rssi-event-from-driver So the only mistake that you might be doing is triggering the network calls in some loop (maybe because a misplaced call or a silly mistake). You can test it on simulator to confirm if its your app only thats causing the issue. If it is, then you need to examine rest of your code to see where are you messing up with the network calls. – paresh May 24 '16 at 11:08
  • Hey.. You were right. I think it was the thread issue. Following code worked, For posting the notification, [[NSNotificationCenter defaultCenter] postNotificationName:@"registerTok" object:self]; For observing it , [[NSNotificationCenter defaultCenter] addObserverForName: @"registerTok" object: nil queue: [NSOperationQueue mainQueue] usingBlock: ^(NSNotification *notification) {[self registerDeviceToken:notification]; NSLog (@"Network went down: %@", notification); }]; – Akshara May 24 '16 at 11:14
  • I am putting it to a queue as it is a part of webservice call that runs in a background thread – Akshara May 24 '16 at 11:18
  • Yeah its correct to add the queue code there. If you don't want to do it there, you can as well move the `postNotification` code inside a `dispatch_async` block to achieve a similar result. – paresh May 24 '16 at 11:21
0

You post the notification with name : @"registerToken" and you observing for @"registerTok" notification. These two names must be the same

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerTok:) name:@"registerToken" object:nil];

Also add the registerTok method

-(void)registerTok:(NSNotification*)notification {
  ...
}
stefos
  • 1,235
  • 1
  • 10
  • 18
0

i think you are not define the selector method registerTok thats way it crashed.

-(void)registerTok:(NSNotification*)notification {
  ...
}
balkaran singh
  • 2,754
  • 1
  • 17
  • 32