The internet often says that the IOS notification mechanism uses the observer model, which has two characters, one is poster (the sender) and the other is observer (the Subscriber that receives the information). But I think the important role is the notification center, which is the core of the entire notification mechanism, the message sent by the poster sender must reach the notification center, and then by the notification center according to which observer subscribers subscribe to the message to those who subscribe to the distribution. The whole can be analogous to the current e-mail structure.
But to add that, although the iOS notice is also called notification Notice, but with the Android notice is not the same, the notice in Android is a message to push the form, and in iOS must also have message push, this is the push mechanism that part of the content. said that this notification mechanism is actually in the learning of C # when the event, but here is the event for the entire system of global events, any side to the system of this global event to register the binding method, to the event is triggered when it can be executed.
The entire notification operation process, we only need to put the poster and the observer to set up to be OK
For the poster operation is relatively simple, he only needs to notify the center to push the notification on it, using the following code
[[Nsnotificationcenter Defaultcenter] postnotificationname:@ "Postone" object:@ "This is Posterone"];
Or
[[Nsnotificationcenter Defaultcenter] postnotification:[nsnotification notificationwithname:@ "PostOne" object:@ " This is Posterone "]];
Notificationwithname:object: The method also has an overload that has the userinfo: argument, which is the Nsdictionary dictionary type, which is used to pass user parameters.
For observer is relatively not poster so simple, the event registration binding in C # also found that triggering an event is just like invoking the method, but the registration party needs to bind the method to the event and define the method, while in the iOS notification mechanism, registration is required, Callback processing (which is the definition of the method in the event) is exhausted and is removed.
Registered
The argument here is with Postnotificationname:object: corresponding. Which notification name parameter is registered to fill in the corresponding value
Callback Processing
-(void) callBack1 (nsnotification*) notification
{
notification.name://notification name
notification.object;// Object notification.userinfo//when notification is sent
UserInfo
}
Delete
After using the message (which is no longer processing the received message), a delete operation is required
[[Nsnotificationcenter defaultcenter]removeobserver:self];//Delete all registered notifications
[[Nsnotificationcenter DefaultCenter] Removeobserver:self name:@ "Postone" object:nil];//Delete notification with name "Postone"
Add:
Observer mode and notification mechanism in iOS, and KVO
In the development of iOS, both notification mechanism and KVO are realized by the Observer mode. The notification mechanism differs from the KVO in that the former is a central object that provides change notification to all observers, who are notified directly to the observer by the observed object.
The following concerns the observer pattern and notification mechanism implementation:
1, the observer mode of subject object, but also can be called the target object, is the notice of the publisher, is also observed. Provide a way to register and unregister; The Observer object, also known as the Observer, is the Subscriber to the notification. Observer class, the acquaintance of subject, to achieve the ability to receive notifications.
2, the notification mechanism in iOS, is cocoa Touch framework for developers of the class, so that developers do not have to write their own observer mode, use it can be achieved. For me, all the uses of the notification mechanism are concentrated in one class. When I used the notification mechanism to understand the observer pattern, I was confused by subject and Observer, who was the sender of the notification, because the class assembled the observer and the sender.