IOS "announcement mechanism" and its understanding of "Architecture mode" and "design mode", and ios message push mechanism

Source: Internet
Author: User

IOS "announcement mechanism" and its understanding of "Architecture mode" and "design mode", and ios message push mechanism

Note: To distinguish between the "local Notification" and "push Notification" iOS reminders, we can see the "Notification", which is translated as "notice" in this article 」. For details about their differences, refer to the iOS development series-notification and message mechanisms.

Problems encountered in practice:

Recently, in a maintenance company project, the following error was reported:-[GlobalManager addAlbum:]: unrecognized selector sent to instance

After investigation, the cause is as follows: previous colleagues putSelf/selfRegister as "Observer" (using addObserver: selector: name: object: method), but not registering the observer class (GlobalManager class) to implement the method in the selector parameter (which is implemented in other classes ). Therefore, the above error is reported (in fact, the error is literally quite clear, but it does not mean the meaning in English ).

Solution: Write the selector Parameter Method in the current class. You can also delete the code for registering the observer. This error is not reported.

After further reading the data, we can conclude that if the addObserver: selector: name: object: method is used to register the observer with the notice center, the second parameter is selector: the method in must be implemented in the current class. If it is written in another class, the above error will be reported. For details, see Figure 1:

Extension: another method for registering an observer
In addition, you can use another method to register the observer:
AddObserverForName: object: queue: usingBlock:
The code is more concise and intuitive. The above method is to use the @ selector keyword to pass the function name of SEL type for callback 」.

Figure 1: register the observer 」


Insert: "callback/callback 」

The above "announcement mechanism" involves the concept of "Callback", because the definition on Wikipedia is a bit abstract and I understand it as a code/function, it must be triggered by a specific user event, that is, "Callback". Without this event, the code will not be executed. The term is "a function called through the function pointer 」.
According to Objective-C Programming 2nd Edition, iOS Callbacks are divided into four categories:
1. Target-action pair/Target-action;
2. Helper Object/Helper objects; (including "Delegate/delegates" and "data Source/data sources 」)
3. Notices/Notifications;
4. Block.
Here, we will simply repeat several types of iOS Callbacks. You can read this article to learn about the situation of callbacks. (I also need more practical experience .)


Explanation: "announcement mechanism 」

We can see that the third callback: "Announcement" is the "announcement mechanism" applied above 」. It is based on the "Observer mode.
At the code level, the notification mechanism involves two classes: NSNotification and nsicationicationcenter. They are all defined in the "NSNotification. h" file.

NSNotification class:
The content carrier representing the "Announcement" has three attributes: name (Announcement name), object (Notification sender/who sends the announcement), and userInfo (additional information/parameters of the announcement)
In addition, the NSNotification class and its extension class (category) have five methods for initializing/instantiating the "Notice". For details, refer to Xcode.

NSNotificationCenter class:
Is the center of the announcement system, which is used to obtain the announcement center, register, remove the observer, and send the announcement. There are eight methods. For more information, see Xcode.

 

Therefore, we can summarizeSteps for applying the "announcement mechanism 」:

1. register the observer.
Solve this problemWHO observes the notice center, what notices are passed, who notices are passed, and what methods are implemented after the notices are received?These problems.
Method for registering an observer
AddObserver: selector: name: object:
AddObserverForName: object: queue: usingBlock:

2. Implement the method in selector or the code in block
In this step, the specific action is taken after the notification is received.

3. Publish/post notices to the notice center.Trigger callback to achieve the final effect. (This step is optional)
Note: Who sends the notification (in which class the notification code is written) depends on the Controller class in which the event is triggered.
In addition, if you observe some system announcements, such as the four notifications of UIDevice (UIDeviceOrientationDidChangeNotification, UIDeviceBatteryStateDidChangeNotification, Token, and token), the system will automatically release the notices without your own implementation.

Method of releasing a notice:
PostNotification:
PostNotificationName: object:
PostNotificationName: object: userInfo:

4. Remove the object registered as an observer from the announcement Center
Available Methods:
RemoveObserver:
RemoveObserver: name: object:

 

Example:

In the AppDelegate class of the project, add the following code to observe/monitor whether there are objects approaching the screen.

1 UIDevice * device = [UIDevice currentDevice]; 2 // enable near-body monitoring function 3 [device setProximityMonitoringEnabled: YES]; 4 5 // register the Observer 6 [[nsicationicationcenter defacenter center] addObserverForName: UIDeviceProximityStateDidChangeNotification 7 object: nil 8 queue: [NSOperationQueue mainQueue] 9 usingBlock: ^ (NSNotification * note) {10 NSLog (@ "an object approaching the screen"); 11}];Implementation of near-body monitoring

 

  

Extension: "Architecture mode/Architectural tural pattern" and "design mode/design pattern 」

At this point, I have a certain understanding of the basic principles of the "Notice mechanism. However, when studying the "announcement mechanism", I came into contact with the "Observer mode" and then the "design mode" and "Architecture mode". Suddenly, the volume of information was too large and I felt like falling into the cloud, only trees, not forests. So I studied for a while (mainly referring to Wikipedia materials) and summarized and drew two figures. I feel like I can grasp these concepts from a macro perspective. (I do not know whether there are any mistakes. If you find any mistakes, please make an axe. Thank you .)

 

Figure 2: Understanding of "Architecture mode" and "design mode"

 

The positions of the "MVC mode" and "Observer mode" in the entire "Software design mode/Software design patterns" are as follows:

Figure 3: Position of "MVC" and "Observer mode/Observer" in the entire "Software design mode/Software design patterns"


Suddenly, I felt quite comfortable.

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.