Search code examples
iosobjective-cnsnotifications

Not receiving NSNotification


I'm having trouble receiving an NSNotification. I can tell that my network call is effective on the server side, and that the server's response is received at the network layer in my app, but when my network layer class does the final step of sending an NSNotification to the UIViewController, that notification is never received. As a result, my app hangs, even though everything else worked as intended.

Here is the UIViewController that initiates everything:

- (void) updateAffiliations
{
    [self.activityIndicator startAnimating];
    self.activityIndicator.hidden = NO;

    Updater *updater = [[Updater alloc] init];
    NSDictionary *clearTextParams = [updater makeDictionary];
    NSString *phpPath = @"updateRecord.php";
    NSDictionary *parameters = [[SharedCipher sharedCipher] encryptParameters:clearTextParams];

    _preferenceUpdateResponse = @"preferencesResponse";

    [[NSNotificationCenter defaultCenter] addObserver : self
                                             selector : @selector(preferenceUpdateResult:)
                                                 name : _preferenceUpdateResponse
                                               object : nil];

    [[SharedNetworkObject sharedNetworkObject] sendHttpPost : phpPath
                                                 parameters : parameters
                                           notificationName : _preferenceUpdateResponse];
    NSLog(@"sent");
}

- (void) preferenceUpdateResult: (NSNotification *) notification
{
    NSLog(@"response received from network layer");
    NSDictionary *dict = [notification userInfo];

    [self.activityIndicator stopAnimating];
    self.activityIndicator.hidden = YES;

    //...
}

The network layer that I refer to is a singleton class called SharedNetworkObject. I've used this class repeatedly, and this is the only instance where this problem is happening:

- (void) sendHttpPost : (NSString *) phpPath
           parameters : (NSDictionary *) parameters
     notificationName : (NSString *) notificationName
{
    NSLog(@"sending http post to : %@", phpPath);
    NSLog(@"params: %@", parameters);

    [self POST : phpPath
    parameters : parameters
       success : ^(NSURLSessionDataTask *task, id responseObject)
             {
                 NSLog(@"response object: %@", responseObject);
                 NSLog(@"broadcasting notification to : %@", notificationName);
                 [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil userInfo:responseObject];
             }

       failure : ^(NSURLSessionDataTask *task, NSError *error)
             {
                 NSLog(@"Shared Network Object error: %@", error);
                 NSDictionary * userInfo = @{ @"result" : @"failure", @"error" : error };
                 [[NSNotificationCenter defaultCenter] postNotificationName : notificationName object:nil userInfo:userInfo];
             }];
}

Here is an example of the console output to show the progress (and then the lack of progress). No errors occur, but the app hangs since the last notification is not received:

sending http post to : updateRecord.php
params: {
ciphertext = "ignore this, lots of encrypted jibberish";
"device_iv" = "0Q7yxsTpzQEaDGPpJ96JrA==";
paddLength = 11;
"session_id" = 845903271;
}
sent
response object: {
message = "successfully updated record";
result = success;
}
broadcasting notification to : preferencesResponse

This output looks fine, except that it is incomplete. There should be one last line that says

response received from network layer

But this never outputs because the notification is never received. I've checked for all of the usual reasons that result in notifications never returning, and I think that this is right. In fact, I use this exact pattern three other times in other places, and they all work perfectly.

Anyone see what I am doing wrong here? Is this maybe a threading issue?

Thanks very much!


Solution

  • I finally figured this out. I had a previous notification observer that had not been removed when this notification was dispatched. I'm surprised that this created a problem since the other notification name was entirely different than this one, but once I reordered the code to remove that other observer before I registered the new one, the problem want away. Live and learn, I guess...

    Thanks for the help, @cbiggin!