0

I am posting a notification when app is coming to foreground state, I am observing notification in each and every view controller. How is the behaviour while observing? Which view controller will observe first and how it works?

Let me give an scenario: I have launched my application and went through the flow, now my app is going to background from the 4th ViewController and coming back to foreground state. Now Which view controller will observe first?

More Details: When I tried, i got the logs as below

2017-01-06 20:54:04.384 notificationCheck[607:7636] Observed in First ViewController

2017-01-06 20:54:04.384 notificationCheck[607:7636] Observed in First ViewController

2017-01-06 20:54:04.384 notificationCheck[607:7636] Observed in Second ViewController

2017-01-06 20:54:04.384 notificationCheck[607:7636] Observed in First ViewController

2017-01-06 20:54:04.385 notificationCheck[607:7636] Observed in Third ViewController

2017-01-06 20:54:04.385 notificationCheck[607:7636] Observed in First ViewController

2017-01-06 20:54:04.385 notificationCheck[607:7636] Observed in Fourth ViewController

How this is happening and what was reason for this? Why the notification is observed many times in firstviewcontroller?Click here to see the code

RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
  • 3
    The notifications will be delivered to all objects that are observing it. The order of delivery is undefined. – Paulw11 Jan 06 '17 at 10:11
  • 3
    just to add to the comment above, only view controllers that are 'loaded' will actually be listening, you should remove the observers as VC's are unloaded – Scriptable Jan 06 '17 at 10:13
  • Possible duplicate of [Send and receive messages through NSNotificationCenter in Objective-C?](http://stackoverflow.com/questions/2191594/send-and-receive-messages-through-nsnotificationcenter-in-objective-c) – Vishal Sonawane Jan 06 '17 at 10:21
  • @Paulw11 If the order of delivery is undefined, why the notification is getting observed more than one time. – SHIVASHANKER KARNE Jan 07 '17 at 06:32
  • Either you have registered your observer more than once or you have multiple instances of your view controllers. Can you show some code? – Paulw11 Jan 07 '17 at 07:42
  • @Paulw11 can you see the image and analyze. I am not adding observer at multiple places in a view controller. – SHIVASHANKER KARNE Jan 08 '17 at 11:31
  • Then you should check your app navigation; you may be creating multiple instances of your view controllers if you always push/show view controllers and never dismiss/unwind. – Paulw11 Jan 08 '17 at 13:44

3 Answers3

0

If a notification observer is added on a UIViewController on a ViewDidLoad method then notification observer must be removed from that UIViewController in a ViewDidUnload or ViewWillDisappear methods. If you don't remove notification observer then every UIViewController where notification observer added will receive the notification event. So thats why It is a good practice to remove a notification observer. Similarly try to avoid add same notification observer multiple times on same UIViewController otherwise that UIViewController will receive the notification multiple time.

Usman Javed
  • 2,437
  • 1
  • 17
  • 26
0

According to this scenario,Order of delivering notifications will be defined.

If you have observers in each and every controllers, the notification delivered in a order how the view controllers are pushed or presented.

If you remove observer in each and every view controller, notification will be observed only in the view controller from where app gone into background state.

-1

Logically when you add a notification and added the observer then remove the notification if your view controller is removed . As per ViewController lifecycle remove in ViewDidUnload method . If you will not remove then every view controller will listen for the notification and start the event if defined .