Five methods of monitoring, observation, and notification in cocoa

Source: Internet
Author: User
Tags notification center
This article describes five methods for implementing the observer mode (also known as broadcaster/listener, publish/register or notify) in Objective C and the value of each method.

This article will include:
1. broadcaster and listeners)
2. Key Value observing)
3. Notification Center)
4. Context notification)
5. Delegate for observation)

About observer
The observer mode is one of the most powerful ways to maintain the abstract relationship between two modules. The observer mode includes a module that publishes an event and several instances of another module that responds to the event. It is different from calling the second module directly, because the first module does not need to focus on the number of observers, so as to achieve a more complete abstract relationship between the observer and the observer.

Manual broadcasters and listeners

Manually, the caster needs to keep an array (nsarray) or set (nsset) of the listener ). When an event needs to be notified to the listener, the broadcaster directly calls related methods on each listener.

In the broadcaster class, you may need an nsmutablearray, nsset, or nsmutabledictionary. Nsmutabledictionary is more suitable for using the event identifier type as the key value for each listener. On the broadcaster, you also need to register the listener and cancel the registration.
The method for sending messages to each object in nsarray or nsset is simple, as follows:

 

[Listenerscollection makeobjectsperformselector: @ selector (methodsupportedbyeverylistener)];

 

 

For more information, see collections programming topics for cocoa.
Advantage: the broadcaster has full control over the listener list.
Disadvantage: manually add or remove listeners in the collection (especially when the listener is not maintained for other reasons ). If you want to publish different messages, you need more manual work.

 

Key Value observation
The key value observation protocol has made great progress in automating the above process. In many cases, broadcasters do not need to do anything.
Each cocoa object is automatically processed by the addobserver: forkeypath: Options: Context: used to publish any object :. If the caster's "setter" method follows certain rules, the "setter" method will automatically trigger the observevalueforkeypath: ofobject: Change: Context: Method of any listener.
For example, the following code adds an observer to the "Source" object ::

 
 
  1. [Source
  2. Addobserver: Destination
  3. Forkeypath: @ "myvalue"
  4. Options: nskeyvaluechangenewkey
  5. Context: Nil];

In this way, an observevalueforkeypath: ofobject: Change: Context: Message to destination will be sent every time setmyvalue: method is called. All you need to do is register the listener on the observed object and enable the listener to implement observevalueforkeypath: ofobject: Change: Context :.

For more information, see nskeyvalueobserving protocol reference. Advantages: built-in and automatic. You can observe any key path. Supports dependency notifications. Disadvantage: the broadcaster cannot know who is listening. The method must comply with naming rules to automatically observe the message operation. The listener must be removed before being deleted. Otherwise, the subsequent notification will cause crash and failure-but this is the same for all methods mentioned in this article.

Notification centerNsicationicationcenter provides a more decoupled approach. The most typical application is that any object can send notifications to the center, and any object can listen to notifications from the center. The code for sending a notification is as follows:

  
  
  1. [[Nsicationicationcenter defacenter center]
  2. Postnotificationname: @ "mynotificationname"
  3. Object: broadcasterobject];

The code for registering to receive notifications is as follows:

 

   
   
  1. [[Nsicationicationcenter defacenter center]
  2. Addobserver: listenerobject
  3. Selector: @ selector (receivingmethodonlistener :)
  4. Name: @ "mynotificationname"
  5. Object: Nil];

You can specify a specific broadcaster object when registering a notification, but this is not required. You may have noticed defacenter center. In fact, this is the only center you will use in the application. The notification is open to the entire application, so there is only one center. There is also an nsdistributednotificationcenter. This is used for inter-Application Communication. There is only one center of this type on the entire computer. For more information, see: Notification programming topics for cocoa advantages: Notification senders and recipients do not need to know each other. You can specify the method for receiving notifications. The notification name can be any string. Disadvantage: More code is required than the key value observation. The listener must be removed before deletion.

Context notificationIf the observed attribute is a declared attribute of nsmanagedojbect, you can listen to nsmanagedobjectcontextobjectsdidchangenotification. The nsnotification method is still used, but it is a little different because nsmanagedobject does not send notifications manually. The registration of this method is as follows:

    
    
  1. [[Nsicationicationcenter defacenter center]
  2. Addobserver: listenerobejct
  3. Selector: @ selector (receivingmethodonlistener :)
  4. Name: nsmanagedobjectcontextobjectsdidchangenotification
  5. Object: observedmanagedobjectcontext];

In receivingmethodonlistener:, key values such as nsinsertedobjectskey, nsupdatedobjectskey, and nsdeletedobjectskey in the userinfo of the notification give a set of affected objects.

For more information, see nsmanagedobjectcontext class reference. advantages: it is the easiest way to track changes throughout nsmanagedobjectcontext. Disadvantage: it is only applicable to core data and does not provide specific information that affects objects.

Delegate for observationThe last cocoa simplified observer mode is delegation. In a broad sense, delegation can not only handle simple observations, but not necessarily require more. For example, all notifications of nsapplication and nswindow are sent to the delegate at the same time for processing. Some Classes send messages similar to notifications to their delegates, instead of sending notifications at the same time. For example, nsmenu sends menuwillopen: it delegates but does not send the corresponding nsnotification. To connect a delegate, you only need to call the following code on the object that supports the delegate:

     
     
  1. [Object setdelegate: delegateobject];

The object can receive any delegate message it wants.

For more information, see cocoa fundamentals guide: delegates and data sources. Advantages: the classes that support it are detailed and specific. Disadvantage: this class must support delegation. At a time, only one delegate can be connected to a certain object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.