Akshara Akshara - 2 years ago 77
iOS Question

NSNotification used between two controllers is crashing

i am using following code


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

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


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

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:url]];
[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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download