Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when to use KVO?

I have read many docs on KVO, but I'm still confused as to when to use it.

In case objA wants to monitor a certain property of objB, like so:

self.objB = [[ObjB alloc] init];
[self.objB addObserver:self
            forKeyPath:@"address"
               options:0
               context:nil];

so if objB's property changes, and it can only be changed by self, why not just do this:

self.objB.property = @"newValue";
[self doSomethingBasedOnNewValueOfObjBnewProperty];

instead of

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context

{
    if(keyPath == @"address") {
        [self doSomethingBasedOnNewValueOfObjBnewProperty];
    }
}

It may be useful when used with a singleton, like self.objB = [ObjB sharedInstance], where properties may be changed by other objects. Is this the only use case?

like image 679
limboy Avatar asked Jun 09 '13 08:06

limboy


People also ask

Why do we use KVO?

KVO, which stands for Key-Value Observing, is one of the techniques for observing the program state changes available in Objective-C and Swift. The concept is simple: when we have an object with some instance variables, KVO allows other objects to establish surveillance on changes for any of those instance variables.

What is difference between KVC and KVO?

Key-Value-Observing (KVO) allows you to observe changes to a property or value. To observe a property using KVO you would identify to property with a string; i.e., using KVC. Therefore, the observable object must be KVC compliant.

Where is KVO used in Swift?

Key-value observing is a mechanism that enables an object to be notified directly when a property of another object changes. It is a mode of communication between objects in applications designed in conformance with the Model-View-Controller design pattern.

What framework is KVO key-value observing a part of?

Key-Value Observing, KVO for short, is an important concept of the Cocoa API. It allows objects to be notified when the state of another object changes.


1 Answers

Not all instances of all classes are created and edited only by the same instance (which is effectively what your example indicates).

KVO is a generic method by which one instance can observe changes on another and receive notifications of what has happened. Those changes could be triggered from anywhere.

Say you added a 3rd party library to your project. You don't know how it's implemented. Calling one method could change multiple different properties in the library class instance. KVO provides an easy way for you to monitor and react to those changes.

KVO also supplies 'Dependent Keys' which allows you to configure relationships between keys so you can say "the value of this property depends on that of one or more other attributes in another object" and KVO will tell you when it happens. This could be particularly useful in a managed object subclass say, if you have a transient key that holds a cached value for some persistent key and the cache needs to be updated whenever the persistent value changes...

like image 95
Wain Avatar answered Oct 29 '22 00:10

Wain