Thursday, April 28, 2016

Objective C Key Value Observer (KVO) Tutorial-1

We need to observe some thing if other things are depending on it. 

Let me list out some scenarios in real time.

  • We may need to observe for total Bill in order to charge service tax or something.
  • We may need to observe file download or upload progress to display the status.
  • We may need observe an array to reload table.
  • We may need to observe the location changes of the device.
  • We may need to observe the orientation changes of the device.

Confused with these observations?


Let's start with a simple example to get a clear idea first. Then we can discuss in detail.

We use NSOperations for background tasks. For example, I have a download operation for downloading some thing from the server.

I have created a download operation. Now I need to observe the below things.

  • Whether operation is ready?
  • Whether operation is executing?
  • Whether operation is finished?
  • Whether operation is cancelled?

We need to observer all these and the reason is quite obvious.

  • If it is executing, I can display the download progress.
  • If it is cancelled, I can perform the relevant steps.
  • If it is finished, I can display the downloaded file or something else.

At this point, I hope you understood,

Why to observer? and practically What to observe?

Let's see how can we write observers for the above discussed items using Objective C.

 - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:
                                           (NSKeyValueObservingOptions)options context:(
void *)context;

Looks familiar right? That's how we add notification observers.

Let's discuss the parameters of this observer.

observer : Could be any thing which comes under NSObject. Technically, where we implement the observer delegate method, which we are going discuss in a while.

keyPath : The property name, which we are observing.

Options, we will discuss in another example.

Let's use the above observer and implement for our operation. I am going to observer 'isFinished'
and 'isExecuting' properties of my download operation.

 [self.downloadToDiskOperation addObserver:self forKeyPath:@"isFinished" 
                                           options:NSKeyValueObservingOptionNew context:NULL];                                                               

 [self.downloadToDiskOperation addObserver:self forKeyPath:@"isExecuting" 
                                           options:NSKeyValueObservingOptionNew context:NULL];                                                      

We are now observing our required properties of our operation. Implement the below method in your observer class to get your observations.

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)operation change: 
                                                              (NSDictionary *)change context:(void *)context 
    if([operation isEqual:self.downloadToDiskOperation]) {
        if ([keyPath isEqualToString:@"isFinished"]) {
              // Display downloaded file.
        else if ([keyPath isEqualToString:@"isExecuting"]) {    
              // Display progress of download.  


In this delegate method, we get the property we are observing and the object, whose property we are observing and also the changes in the property.

In this case,

isFinished is initially NO. When the download is done, It becomes YES and the observer delegate method will get called.


Hope this post is useful. Feel free to comment incase of any queries.

Objective C Key Value Observer (KVO) Tutorial-2