Broadcasting Information: How to use the iPhone Notification Center


There are times when building an iPhone application that you
need to inform other parts of the program to do something, without regard to
what or where those parts are. The NSNotificationCenter (aka “notification
center”) is the way to go in these cases. Using notifications supports ‘loose
coupling’, which is a good thing in software design.

A notification center object provides a mechanism for
broadcasting information (“notifications”) within a task. An
NSNotificationCenter object is basically a notification dispatch table. For
those familiar with Windows programming, this pattern is similar to the Windows
Message Loop architecture.

For example, one object may be performing a long-running
operation (such as downloading a file) and needs to inform view controllers to
change the view to indicate this busy state. While we could pass around a bunch
of object references, it is much easier to post a notification and let the listening
objects decide what to do.

First, we’ll register with a notification center to receive
notifications. Here’s the call:

[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(myMethod) name:@”DoSomething”object:senderICareAbout];

So what’s going on here? First, we get a reference to the
default notification center for the current task (application). Then we add
ourselves (the calling object) as an observer of the notification.

The selector specifies the method we want to call when this
notification arrives. The selector method takes one argument, which is an NSNotification
object. It contains information about the notification, such as the name and
sender object.

The notification name is the ‘message’ we are listening for.
This parameter can be nil if we want to receive all notifications.

Finally, the object parameter is used to limit incoming
notifications to those from a particular object. This can also be nil if you
don’t care where the notification came from.

Now that we have an object listening, we can send (“post”) a
notification. Here’s the code to do that:

[[NSNotificationCenterdefaultCenter] postNotificationName:@”DoSomething”object:self];

The parameters here are pretty straightforward. We post the
name of the notification, and the sender object (self in this case). If we
don’t care about the sender object, we can use:

[[NSNotificationCenterdefaultCenter] postNotificationName:@”DoSomething”]

which will set the sender object to nil in the received

A notification center delivers notifications to observers
synchronously. In other words, the postNotification: method does not return
until all observers have received and processed the notification. If you want
to send notifications asynchronously, you should use a notification queue (NSNotificationQueue).
We’ll save that discussion for another day.

A note of caution: in multithreaded applications, notifications
are delivered in the same thread that the notification was posted, which may
not be the same thread in which an observer registered, so be careful. If you
are threading a long process, you should send the notification in the main
thread, not the thread created for the process.

Now you have the information you need to start broadcasting notifications
to your hearts content. This will help keep your application code mean and clean.

Leave a Reply

Your email address will not be published. Required fields are marked