开发者

NSNotificationCenter vs custom observer Protocols

开发者 https://www.devze.com 2023-01-05 04:51 出处:网络
When a view must listen the changes on the model and updates view, in other languages we must create and implement observer methods. The view registers itself on that observer methods and wait for the

When a view must listen the changes on the model and updates view, in other languages we must create and implement observer methods. The view registers itself on that observer methods and wait for the notifications.

This can be do in Obj-C with protocols and registering objects. When some value changes we iterate over our observers list and perform a selector if the class implement it. Pretty straightforward.

But Obj-C has a built-in notification system that do this process too less heavy. Simply a class register itself as observer of certain notifications and the model posts this notification when the certain value is updated.

My questions are: what is the better approach to implement the observer pattern? Using NSNotificationCenter or making our custom Protocols? H开发者_如何学Pythonas NSNotificationCenter a high computing time that affects the performing of the app? Or simply don't use NSNotificationCenter is a question of nostalgia?


NSNotificationCenter vs delegation( using protocols )?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号