Notification mechanism in iOS

  • 2020-05-30 21:10:09
  • OfStack

It is often said on the Internet that iOS's notification mechanism USES the observer mode, with two roles, one being poster(the sender) and the other observer (the receiving subscriber). But I think the important role is the notification center, which is the core of the whole notification mechanism, where messages sent by poster senders must go to the notification center, and then the notification center distributes the messages to those subscribers based on which observer subscribers have subscribed to the message. The whole can be compared to the current E-mail structure.

However, in addition, notice of iOS is also called Notification notice, but it is different from notice in Android. Notice in Android is a form of message push, and there must be message push in iOS, which is the content of the push mechanism. The notification mechanism is actually the event when we were learning C#, but the event here is a global event for the whole system. Any one party registers the method to the global event of the system, and it can be executed when the event is triggered.

For the whole notification operation process, we only need to set up poster and observer

The operation of Poster is relatively simple. It only needs to push the notification into the notification center, using the following code


[[NSNotificationCenter defaultCenter] postNotificationName:@ " PostOne "  object:@ " This is posterone " ];

or


[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@ " PostOne "  object:@ " This is posterone " ]];

Another overload of the NotificationWithName: object: method is the userInfo: parameter, which is the dictionary type of NSDictionary, which is used to pass user parameters.

For Observer relatively Poster so simple, event registration in C # binding also found that when the triggering event just like call methods that call is ok, but that one party requires registration for the event binding approach, and to define the method, and in iOS notification mechanism, need to register, the callback handling the method definition (event), ran out to delete.

registered


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack1:) name:@ " PostOne "  object: nil]; 

The parameter here is corresponding to postNotificationName: object:. Which notification name parameter is registered is filled in with the corresponding value

The callback processing


-(void) callBack1(NSNotification*)notification
{
notification.name:// Name of notice 
notification.object;// When the notification is sent object
notification.userInfo// When the notification is sent userInfo
}

delete

After the message has been used (that is, it is no longer processed for the received message), the deletion operation needs to be performed


[[NSNotificationCenter defaultCenter]removeObserver: self];// Delete all registered notifications 
[[NSNotificationCenter defaultCenter]removeObserver:self name:@ " PostOne "  object:nil];// Delete name as" PostOne "Notice of 

Supplement:

The observer mode and notification mechanism in ios, and KVO

In the development of ios, both the notification mechanism and KVO are implemented in the observer mode. The notification mechanism differs from KVO in that the former is a central object that provides notification of changes to all observers, and the latter is the observed object that sends notification directly to the observer.

The following focus observer pattern and notification mechanism implementation:

1. The Subject object in the observer mode, also known as the target object, is both the publisher of the notification and the observed. Provide methods for registration and unregistration; The Observer object, also known as the observer, is the subscriber of the notification. In the Observer class, know Subject in order to be able to receive notifications.

2. The notification mechanism in ios is a class developed by the Cocoa Touch framework for developers, which enables developers to use it without having to write observer mode. For me, all the use of the notification mechanism is concentrated in one class. As a result, when I used the notification mechanism to understand the observer pattern, I was confused about Subject and Observer, who was the sender of the notification, because the class was composed of the observer and the sender.


Related articles: