IOS-Observer Mode

Source: Internet
Author: User
Tags notification center

Let's talk about small examples in our daily life. When we are still young, We need to drink milk. In order to drink fresh milk, we often go to a store dedicated to ordering milk and put our residential address, the brand of the purchased milk also tells the person in charge of the store that we can drink fresh milk every day. Well, we don't need to go anywhere, the person in charge of the store will personally give you the milk.

We can also find that this can also avoid direct connection between "Producers" (milk manufacturers) and "consumers" (US), which is actually to reduce the coupling between the two modules. The milk manufacturer delivers the milk of each brand to the store where the milk is sold, and then transmits the milk to our "consumer" through the consumer's ordering information (milk brand, residential address.

 

In IOS development, you can use notifications to implement this function. If you have the opportunity to contact KVO in the future, let's make a conclusion.

Registration notification

[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (sendMilk :) name: @ "quechao" object: nil];
A store that subscribes to milk is like a notification center in IOS. It registers a notification named quechao and uses self as the observer, that is, when a message named quechao is sent (the manufacturer sends the quechao-branded milk to the store), The sendMilk method is executed to send the milk to the consumer.

Send message

[[Nsicationcenter center defacenter center] postNotificationName: @ "quechao" object: milk]; The manufacturer sends the quecao-branded milk to the store. After receiving the notification, execute sendMilk: Method

-(Void) sendMilk :( NSNotification *) notification
{
// Code
}

The advantage of the notification method is that it separates the relationship between the producer and the consumer. But in this case, it is often too bad, just like this, if there is no connection at all, it will easily lead to poor code readability. When there are many notifications, reading may be complicated and there is no idea.

 

Related Article

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.