Basics of the NSNotificationCenter

The uses of NSNotificationCenter can be many; It is handy for using to notify the app that a background download of data, a post of some kind is done, or some calculation is finished. It can also be useful for letting sub views know when some root level functions happen in the app such as shutdown/sending to background because the user tapped the home button. In this example I will be doing just that.

In the AppDelegate you should find a method named:

- (void)applicationDidEnterBackground:(UIApplication *)application

This is called when the app starts the process to pause into the background state. Since this method can only be called by the app delegate and not a sub view notifications are ideal for subviews so that they can do what they need such as save data or stop heavy processing of data.

//.m
- (void)applicationDidEnterBackground:(UIApplication *)application{
   [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationDidEnterBackgroundNotification" object:self];
}

'UIApplicationDidEnterBackgroundNotification' is the name of the notification we send out. If we name notifications in a smart way it makes sense when you have lost of them to listen for.

In a sub view class we need tell it to listen (observe) for the 'UIApplicationDidEnterBackgroundNotification'. Add the notification observer to somewhere like the init so that it is listening as soon as the object is created. Lets go over the details before we see the code.

addObserver:self assigns the listener to the current object. We can also assign the listener to other objects, such as views, i.e: addObserver:myCustomViewControllerObject.
selector:@selector(theActionToCarryOut:) is which method to call when we get the notification.
name:@"UIApplicationDidEnterBackgroundNotification" is what notification we are listening to.
object:nil is which object sent it. By setting 'nil' we say that we don't care where the message came from. If we specified 'myCustomObject' then only messages from this object would call the action.

//.m
//A generic init for something
- (id) init {
   self = [super init];
   if (!self) return nil;
   [[NSNotificationCenter defaultCenter] addObserver:self
   selector:@selector(theActionToCarryOut:) 
   name:@"UIApplicationDidEnterBackgroundNotification"
   object:nil];
   //there could be lots of observers here for different events.
   return self;
}
 

Then create the method to do something when we here the message.

.h
- (void) theActionToCarryOut:(NSNotification *) notification;

.m
- (void) theActionToCarryOut:(NSNotification *) notification {
   NSLog(@"The app just went into the background");
   //Shut down the memory/processor intensive things and save any states for when the app is reinitialised
}

We also need to remove the listener once we no longer need it, for example if the view is removed:

- (void)viewDidUnload {
   [[NSNotificationCenter defaultCenter] removeObserver:self];
}
Published on

Discuss!

comments powered by Disqus