61 (OC) * Acting block notification agent kvo

1. Understand the difference between block and from the source and delegate

Delegate low operating costs, high operating cost of the block.

A data block from the stack needs to be used to copy the stack memory heap memory, then of course is to add the object count, is set using the finished block or nil after elimination. delegate only holds a pointer to the object, direct callback, there is no additional consumption. Like the C function pointer, just do a look-up table action.

2. Use a scene from the difference between block and delegate

There are a number of related methods. Each method is provided if a block, it will be more troublesome. The delegate allows multiple methods into a group, you need to set only once, you can repeatedly callback. When more than three methods should be given priority delegate. When 1,2 callback, use block.

delegate safer, such as: avoid circular references. Little attention is formed using a circular reference block, resulting in the release of the object can not. This circular reference, in the event it is more difficult to check it out. The method delegate is separate from and does not reference context, and therefore safer.

Parameters callback delegate returns is limited within the scope of NS classes, the numbers are very limited (of course, can be called directly in the form of bypass method is not recommended; you can also use Array sets the pass, but the need for such a document support , or not clear enough, the callback method also requires independent verification, it is not recommended).

Efficiency is definitely higher than the delegate NSNotification.

KVO provides a mechanism, when the properties specified for the image to be observed is modified, automatically KVO notification response observer, KVC (key code) is the basis of KVO

KVO use: to be watched happen addobserver: forkeyPath: options: context:. Method to add an observer and then as long as the change in the value of keypath observer (note that simply changing the value of the method is not called, only to change values ​​through getter and sett will trigger KVO), will call a method on the observer in observerValueForKeyPath: ofobject: change: context: therefore implement this method of KVO issued notice to respond.

The observed without adding any code, so anyone who listens anyone who registered, then the response to treatment may be performed so that the observer and the observed complete decoupling, very flexible and very easy to use, but can only detect KVO class property, and attribute names are referred to by their NSString, the compiler will not help you determine the right and wrong and completion, will be relatively pure hand knock error prone

 

Notify: a broadcast mechanism, in practice occurs when, by the notification target, a target object of interest is possible for all the time of occurrence of the transmission message, both the observer pattern, except that the observer is directly transmitted is KVO message to the observer, is the direct interaction between objects, notice is to inform both the center and object interaction between objects do not know each other

NSNotification characteristic is the need to be proactive notification observer, then the observer after registration listens, responds again, more than a step to send notifications KVO, but its advantage is not limited to changes in the properties of the monitor, but also on a variety of a variety of changes in the state to monitor, monitor a wide range of use and flexible.

Singleton

A singleton class, only one instance of the entire program, and provides a class method for a global call, initialize the class at compile time, and then has been stored in memory, the program (APP) automatically releases this memory from the system exit .

The UIApplication (application instance class)

NSNotificationCenter (Class Message Center)

NSFileManager (File Management)

NSUserDefaults (application settings)

A NSURLCache (request cache class)

NSHTTPCookieStorage (application cookies pool)

 

Guess you like

Origin www.cnblogs.com/zyzmlc/p/11411711.html