One of the frequent pain points of iOS programming is dealing with
NSNotificationCenter. It is has very “bulky” syntax, you must juggle a lot of constant string, and make sure you add and cleanup your observers properly. All this causes a lot of boiler plate code, and worst part is that it is all over the place:
As you see, three different places are littered with
NSNotificationCenter dependency, not to mention this bulky API. Not only that, but you may mistakenly observe an event that sends notification on a background thread, then update your UI … BOOM. So, now you have to wrap it around
dispatch_async … This is getting really ugly.
We want to do something about that…
That’s where Notificationz comes in. It is super light-weight and simple, and just aims to solve this problem. We introduce only 2 classes, and you go from the previous syntax to this:
That’s it! The code above shows you 3 important concepts:
You don’t have to do any cleanup yourself. When this object is dealloc’d, the observer variable will automatically get dealloc’d and remove the observer from
NSNotificationCenter. If you like, you can prematurely remove the observer by simply setting the value of the variable to
Ensure Main Thread
The library provides
observeUI. As the name implies, observing for UI will guarantee that the notification block will be executed on the main thread. This means you can safely update your UI from the callback block.
Now, if you want to know what this class observes, all you do is look at the instance variables. They tell you all you need to know about what this class observes.
Also, once you observe a notification name, you immediately follow with the handler code. This makes sure that you don’t deal with “strings” that may easily break when the selector is removed for renamed.
The library offers other helpful functions that complement the