Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How NSNotification works

I understand what in Notification, poster and observer.

But I am quite unable to understand how our app or OS understands and sends the flag/notification to the observer-class?

What is the mechanism behind this?

Your answer and help will be appreciated a lot.

Thanks

ID.

like image 699
Ileana D'Cruz Avatar asked Jun 30 '13 07:06

Ileana D'Cruz


People also ask

What is NSNotification?

An object containing information broadcast to registered observers that bridges to Notification ; use NSNotification when you need reference semantics or other Foundation-specific behavior. iOS 2.0+ iPadOS 2.0+ macOS 10.0+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+

What is NSNotification Center in Swift?

A notification dispatch mechanism that enables the broadcast of information to registered observers. iOS 2.0+ iPadOS 2.0+ macOS 10.0+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+


2 Answers

Imagine the Notification Center as a dictionary which has keys of the notification names and values of the list of observers (and their specified action methods). When a notification is posted, the list of observers for that notification name is obtained and iterated. Each observer has its action method called with the notification information.

Also, during the iteration there is a check to determine if the notification object is of interest to the observer (based on the parameters supplied when the observer was added).

The notification process is carried out on the thread from which the notification was posted.

Don't think about trying to rely on any implied order related to how and when the observers were added.

like image 107
Wain Avatar answered Oct 02 '22 22:10

Wain


Basically the NotificationCenter keeps a reference to any object that is registered as an observer. With that reference, it also keeps track of what kind of notifications that object wants. When an object posts a notification, the center delivers it to each registered observer by sending the observer a message with that selector.

The default center is normally a global singleton. But you could create your own, perhaps if you want to ensure your notifications are private to your app.

like image 22
uchuugaka Avatar answered Oct 02 '22 22:10

uchuugaka