"ios" notification mechanism "and its understanding of" architectural pattern "and" design pattern "

Source: Internet
Author: User

Note: In order to distinguish between "local notifications" and "Push Notifications", the "notifications" that are visible to users in iOS, this article translates notification into "announcements." Their detailed differences can be found in the article "iOS Development Series-Notification and messaging mechanism".

Problems encountered in practice:

Recently in a project to maintain the company, encountered such an error:-[globalmanager addalbum:]: Unrecognized selector sent to instance

The reason for this is as follows: Former colleagues used the "notification mechanism" to register their " /self" as "observers" in the Globalmanager class (using the AddObserver:selector:name:object: method), but there is no way to implement the selector parameter in the class (Globalmanager Class) that registers the observer (he implements it in other classes). So the report of the above error (in fact, the errors are literally said to be quite clear, but the total sense of not the meaning of English).

Workaround: Write the method of the selector parameter in the current class. or delete the code that registers the "observer", that is, this error is not reported.

After the subsequent access to the information, concluded: If the AddObserver:selector:name:object: method to "Notice Center" REGISTER "Observer", the second parameter, that is, selector: The method, must be implemented in the current class, if written in other classes, The above error will be reported. See Figure One:

Extension: Another way to register an observer
In addition, a registered observer can use another method:
AddObserverForName:object:queue:usingBlock:
This is a "callback" in the form of block, and the code is more concise and intuitive. The method above is called "callback" by using the @selector keyword to pass the SEL type's function name.

Figure One: Register "Observer"


Interrupt: "Callback/callback"

The above "notice mechanism" involves the concept of "callback", because the definition on Wikipedia is a bit abstract, I understand that: a code/function that needs to be triggered by a specific user event, or "callback", without this event, will not execute this code. The term is the function called through the function pointer.
According to the book "Objective-c Programming 2nd Edition", the iOS callback is divided into four categories:
1. Target-action to/target-action;
2. Helper Object/helper objects; (including delegate/delegates", data source/data sources")
3. Circular/notifications;
4.Block.
Here are just a few examples of iOS callback types, as to what kind of callback can be described in the book. (I also need more practice to experience myself.) )


Explanation: "Notification mechanism"

As you can see, the third callback: "Notice" is the "notification mechanism" we applied above. It is based on the "observer pattern".
The "notification mechanism" at the code level involves two classes: the Nsnotification class and the Nsnotificationcenter class, all defined in the "NSNotification.h" file.

Nsnotification class:
The content carrier that represents the "notice" has three attributes: name (the name of the advertisement), object (the sender of the advertisement/who sent the notice), UserInfo (additional information/parameters for the announcement)
In addition, the Nsnotification class and its extension Class (category) also have 5 methods for initializing/instantiating the "advertisement", which can be viewed in Xcode.

Nsnotificationcenter class:
is the center of the notification system for getting notification hubs, registering, removing observers, and sending announcements. There are 8 methods. Details can be viewed in Xcode.

Therefore, we can summarize the steps to " apply" the notification mechanism:

1. Register the viewer.
This step addresses the issue of who observes the notification center, observes what notices are communicated, observes who notices, and what methods are executed after receiving the notice .
How to register an observer
AddObserver:selector:name:object:
AddObserverForName:object:queue:usingBlock:

2. Implementing a method in selector or code in a block
This step specifically implements what actions are performed after the notification is received.

3. Issue a/post notice to the Notification Center. triggers a callback to achieve the final effect. (This step is optional)
Notice who is sending the advertisement (in which class the notification code is written) depends on which controller class the trigger event is in.
In addition, if you observe some system announcements, such as the uidevice of these four kinds of "notice" (Uideviceorientationdidchangenotification, Uidevicebatterystatedidchangenotification, Uidevicebatteryleveldidchangenotification, Uideviceproximitystatedidchangenotification), it is automatically advertised by the system and does not need to be implemented by itself.

How to publish an advertisement:
Postnotification:
Postnotificationname:object:
PostNotificationName:object:userInfo:

4. Move an object registered as an observer out of the Notification center
Available methods:
Removeobserver:
RemoveObserver:name:object:

Example:

In the Appdelegate class in the project, add the following code to observe/monitor if an object is close to the screen.

1Uidevice *device =[Uidevice Currentdevice];2 //turn on close watch function3 [device Setproximitymonitoringenabled:yes];4 5 //Register Observer6 [[Nsnotificationcenter Defaultcenter] Addobserverforname:uideviceproximitystatedidchangenotification7                                                   Object: Nil8 Queue:[nsoperationqueue Mainqueue]9usingblock:^ (Nsnotification *Note) {TenNSLog (@"There's an object approaching the screen."); One}];
realization of near-body monitoring function

  

Extension: "Schema mode/architectural pattern" and "design mode/design pattern"

To this, the "notification mechanism" of the basic principles, concrete implementation has a certain understanding. However, in the study of the "notification mechanism", access to the "Observer mode", and then touch the "design mode" and "architecture mode", suddenly information is too large, feel like falling clouds, saw trees, trees. So I studied for a while (the main reference to Wikipedia), summed up and draw two, feel can probably from the macro grasp these concepts. (I do not know if there is any fallacy, if found, please treatise, thank you.) )

Figure II: Understanding of "architectural patterns" and "Design Patterns"

As for the position of the "MVC mode and the Observer pattern in the/software design patterns" of the whole software designing mode, three:

Figure three: "mvc" and the location of the "Observer pattern/observer" throughout "The software design pattern/software the patterns"


All of a sudden, I feel a lot more comfortable.

"ios" notification mechanism "and its understanding of" architectural pattern "and" design pattern "

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.