NSNotificationCenter Notifications

This kind of notifications is not for a user, but for the app. You can inform your objects about some important things, for example about some task that has been started or finished, about some view that has been shown or hidden, etc.

NSNotificationCenter Notifications can be sent and received by any objects. Notifier object can have multiple listeners.

To send a notification you can use (as always) 2 types of code. Clean Objective-C code:

        # empty notification:
        NSNotificationCenter.defaultCenter.postNotificationName "NotificationName", object: self

        # notification with some data:
        data = { is_online: true }
        NSNotificationCenter.defaultCenter.postNotificationName "NotificationName", object: self, userData: data
      
First we get an instance of an NSNotificationCenter object with NSNotificationCenter.defaultCenter, and then we use it to post notification. Notification name is very important because all object that you want to be notified will have to listen to the notifications with the same name.

With sugarcube, your code would look prettier, but first you will have to add 'sugarcube-notifications' package in your Gemfile. Then you can use the following syntax to send notifications:

        # empty notification:
        "NotificationName".post_notification(self)

        # notification with some data:
        data = { is_online: true }
        "NotificationName".post_notification(self, user: data)
      
Technically, sugarcube have added post_notification method to the String objects. When you call it, sugarcube creates a Notification from a provided string and passes it to NSNotificationCenter.

Ok, so we know now that it is very easy to send a notification. But how can we add some custom actions when notification has been sent? Very easy as well - we just tell objects to call some method when they get notified.

First we need to tell them to observe notifications with a given name, and provide the name of the method that will be called when notification received.

It is also important to stop listening for notifications when the object is going to be deallocated. If you are going to listen for notifications inside a screen, it would be a good idea to start listening for notifications in on_appear and remove observers in on_disappear:

        def on_appear
          # start listetning:
          NSNotificationCenter.defaultCenter.addObserver self,
            selector: 'notification_received:',
            name: "NotificationName",
            object: nil
        end

        def on_disappear
          # don't forget to stop listening:
          NSNotificationCenter.defaultCenter.removeObserver(self)
        end

        def notification_received(notification)

          # if you have passed some data:
          user_data = notification.userInfo
          user_data[:is_online]

        end
      
Traditionally, sugarcube code:
        def on_appear
          # start listetning:
          "NotificationName".add_observer(self, :notification_received)
        end

        def on_disappear
          # don't forget to stop listening:
          "NotificationName".remove_observer(self)
        end

        def notification_received(notification)

          # if you have passed some data:
          user_data = notification[:user]
          user_data[:is_online]

        end
      

Summary

NSNotificationCenter Notifications are a great tool that allows objects to communicate inside your app. You can send a notification from a Model and Receive it in your Controller or View. Or in both of them at the same time!

But try to use them only when you really need it. Code with this type of notifications is more difficult to debug.

As usual, you can read more about NSNotificationCenter at Apple Docs.

Book Index | Next