Using NSNotificationCenter in Swift

Image for post
Image for post

One use of NSNotificationCenter is as a tool for communicating information within your App, as a broadcast framework that acts similarly to KVO. A GitHub repo contains the example in this post https://github.com/stevencurtis/simplenotificationexample

Image for post
Image for post

Prerequisites:

  • Some experience of OO concepts might be useful

Terminology

KVO: Key Value Observing allows one object to watch a key path on another object and be notified when it changes

NotificationCenter: A notification dispatch mechanism that enables the broadcast of information to registered observers

The problem

iOS uses NotificationCenter.default to post all notifications and observe them. Each notification requires a unique name, and this can be a method for a model to communicate with a view controller or any one to many relationship (since we think of this as a broadcast type of messenging).

Example

This example is simply two view controllers within a navigation controller.

We traverse from the the main view controller “ViewController” to “DetailViewController” through a segue, and then pass back the option chosen through NSNotification and display the results in a stack view.

The notification name must be unique: “dataModelDidUpdateNotification” and can be set up in viewDidLoad

and when we observe a change, we will add a new label to the stack view. It should be noted that this function has to be marked with @objc so it is accessible from Objective-C code. We have also prepared this notification to receive data, called notification in this example:

We set up an extension for the notification name to make this a little less fragile, as looking simply at the Strings is likely to be error-prone and cause problems.

Over on the detail view we set up a send notification function (which is then called from our button presses):

The data is sent as a dictionary object.

Issues with the example

If you remove the observer from viewWillDisappear through

NotificationCenter.default.removeObserver(self)

ViewController will never update. This is because there is no model for ViewController to update from, so this should come as no surprise. We would usually expect when we select an option for the model to be updated, and if ViewController is visible to update through a NSNotificationObserver and if it is not, to update the whole interview on viewDidLoad.

This example is so small that it causes inherent problems!

Also it should be mentioned that yes it is possible to use a tableview rather than a stackview to display the chosen options. You are right.

Want to access that simple example? You can go here: https://github.com/stevencurtis/simplenotificationexample

Want to get in touch? Use the link here:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store