NSNotificationCenter vs delegation( using protocols )?

前端 未结 6 612
天涯浪人
天涯浪人 2020-11-27 10:56

What are the pros and cons of each of them?
Where should I use them specifically?

相关标签:
6条回答
  • 2020-11-27 11:20

    The rule of thumb here is how many clients would like to be notified of an event. If it's mainly one object (e.g. to dismiss a view or to act upon a button clicked, or to react to a failed download) then you should use the delegate model.

    If the event you emit may be of an interest to many objects at once (e.g. screen rotated, memory usage, user login/logout), then you should use the NSNotificationCenter.

    0 讨论(0)
  • 2020-11-27 11:24

    Notifications are generally better for notifying the UI of changes the occur on other threads as well. Apple's documentation strongly discourages the use of delegates across threads where possible, both for stability and performance reasons. On the Mac, they suggest using Bindings, but since they don't exist on the iPhone, notifications are probably your next best bet.

    0 讨论(0)
  • 2020-11-27 11:25

    Their purposes are different:

    • Notification is used to broadcast messages to possibly several recipients unknown from the sender.

    • Delegation is used to send messages to a single known recipient acting on behalf of the sender.

    0 讨论(0)
  • 2020-11-27 11:36

    Considering the performance is a good idea (delegation better for small number of notified objects, notification centre better for larger number of objects, or is it? run a profiler) but I think a more important factor since you are talking about Objective-C and less likely to be talking about the really high performance parts of your code base, which are likely to be written in C, is reducing compile-time dependencies between modules.

    There is nothing to stop you having an array of delegates rather than a single delegate.

    I might use NSNotificationCenter only for status of any network stack components I make and any custom device status monitoring interfaces. But for most coupling, not to do with global status of the app, I think it is clearer to use normal interface contracts in Objective-C in most cases and easier to folow for the people coming after you than to use NSNotificationCenter. In fact I have never used NotificationCenter for my own custom events and prefer to use delegates for ease of code comprehension by someone else reading my code.

    And finally, of course with notifications to/from the standard API you don't have choice and must use whichever of the two methods Apple proscribe for a given event.

    0 讨论(0)
  • 2020-11-27 11:43

    Notifications are better for decoupling UI components. It allows you to plug any view without any modification in your controllers or models. Definitely better for loosely-coupled design.

    But for the performance between delegation and notification, you need to think about the frequency of the call.

    Delegation might be better for more frequent events, notifications are better for less frequent events but more recipients. It's up to project what to pick.

    0 讨论(0)
  • 2020-11-27 11:45

    An option in between those two is using the observer pattern, without NSNotificationCenter. Look at my Objective-C implementation here.

    0 讨论(0)
提交回复
热议问题