Wednesday, August 26, 2015

Safer Block-based NSNotificationCenter API

Arkadiusz Holko:

I wanted to learn how Clang decides when to show a warning, so I did what any reasonable person would do and dove into its source. Retain cycle checking is performed during the semantic analysis phase. Sema::checkRetainCycles is an overloaded method responsible for these checks.

[…]

In case of a message send, the compiler checks for retain cycles only when a selector looks like a setter, i.e., it starts with the word add or set. This check is rather simplistic. A warning isn’t presented to the user when using NSNotificationCenter, because the compiler doesn’t know the lifecycle of that object.

So he made a MCSNotificationController wrapper so that the compiler can detect the retain cycle. It also makes sure that you don’t register for the same notification more than once and automatically unregisters in -dealloc.

Previously: NSNotificationCenter With Blocks Considered Harmful, How Not to Crash #3: NSNotification.

Comments RSS · Twitter

Leave a Comment