What's the better way to addObserver/removeObserver with NSNotificationCenter?

前端 未结 5 1403
孤城傲影
孤城傲影 2020-12-08 15:07

I used to addObserver in viewDidLoad: and removeObserver in dealloc:. Code:

- (void)viewDidLoad
{
    [su         


        
相关标签:
5条回答
  • 2020-12-08 15:35

    I would normally put it in -viewDidAppear: and -viewDidDisapear: (or -viewWillAppear: and -viewWillDisappear:) simply because in every case I came across I'm only interested in the notification if the view is actually displayed.

    It's probably a premature optimisation (your code for handling the notification could take some time, but might be useless if the view is not displayed), but then it's also no more code - it's the same code just in a different method...

    0 讨论(0)
  • 2020-12-08 15:36

    -viewWillAppear: + -viewWillDisappear: is better than -viewDidAppear: + -viewDidDisapear:, because they are always called the same number of times.

    0 讨论(0)
  • 2020-12-08 15:39

    Don't forget NSKeyValueObservingOptionInitial. I use it with viewWillAppear/viewWillDisappear so my UI is always up-do-date, even if I hide that view controller, saving resources because I will not update it until is shown again.

    0 讨论(0)
  • 2020-12-08 15:44

    this depends on your scenario, usually the best approach is to add in viewDidLoad and remove in dealloc and in viewDidUnload (deprecated in iOS 9.0, use dealloc only), but there are some cases when you have same method in different classes like UI effects and want to call only current screen's method using notification, then you will have to add the observer in viewWillAppear and remove it in viewWillDisappear or viewDidAppear/viewDidDisappear

    Edit: A note from comments, thanks @honey.

    Though now since iOS 9, you no longer need to care about removing the observer. See Apple release notes: "In OS X 10.11 and iOS 9.0 NSNotificationCenter and NSDistributedNotificationCenter will no longer send notifications to registered observers that may be deallocated..

    0 讨论(0)
  • 2020-12-08 15:44

    The best approach using NSNotifications is adding the observer when you need to observe for notifications and remove them when you don't need them anymore.

    This could be on viewDidLoad:, viewWillAppear:, or when the user taps some button etc.

    I will give you a little example:

    My app has a tabbar, and in some of the view controllers, I'm displaying some info downloaded from internet (a tweet for example). I also have a class pooling for new data from server each 2 minutes, and as the server had new data, I updated the info on database. I will not use a delegate pattern to listen to DB changes, because I have so many view controllers displaying data, and it will be a very bad design making the delegate an array and looping to pass the data for every view controller. So, in this specific scenario, the best to do is to post a notification telling every VC that new data has come.

    If your VC removes the delegate when the view disappears, only the current one will receive the notification and update the displaying contents.

    You obviously could update the contents of the other VCs before display, on viewWillAppear: for example, but doing this the VC contents will be updated not only when necessary, but each time you change tabs.

    It was only one example, where I tried to show you that for NSNotifications, is difficult to advise you when to add or remove observers when we don't have the entire description of how you app behaviours.

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