Observer Pattern

Dropping NSNotifications and KVO

Alexey Kuznetsov
1 min readNov 9, 2015

NSNotificationCenter implements the publish-subscribe pattern. Its main advantage—decoupling of publishers and subscribers—is also its main disadvantage. There’s little help from the compiler, it’s hard to follow the app flow, it’s a pain to test.

KVO is not an ideal API either. Just looking at the code, it’s impossible to tell which property is observable and which is not.

Over time, I’ve become hugely dissatisfied with both and switched to the classic observer pattern.

Additional benefits: can be used in pure Swift, no dependency on the third-party frameworks.

In the next post, I’ll describe how multiple observers fit into this approach.

Related Articles

--

--