Showing posts with label NSKeyValueObservingOptions. Show all posts
Showing posts with label NSKeyValueObservingOptions. Show all posts

Thursday, April 28, 2016

Objective C Key Value Observer (KVO) Tutorial-2


Why we need observers,
How to observe property changes,

by taking NSOperation properties as examples.

In this post, Let's take a real time practical example and discuss KVO in detail.

I am taking an example of Employee object, where his/her salary says whether that employee comes under tax or not. This clearly says, we need to observe for employee salary to play with his tax calculation.


                         



I am taking an Employee class which is as shown below.















Employee class is having two properties here, salary and isTaxApplicable

isTaxApplicable is read only as it is depending on the salary. You can't set it's value from out side of this class as it is completely depends on the salary value.

But in my implementation file, I am going to write an extension as I need to change the access level of isTaxApplicable property inside my class as shown below.










In the extension, I have changed the property to readwrite as without which I can't set it's value inside my class.

If this 'extension' is sounding new/strange to you,


Please check out my tutorial Objective C Extensions and do come back here once you get an idea on Extensions topic in Objective C :)

We need to write the observer on salary property and in the observer delegate method, we need to change the isTaxApplicable property based on the salary value. So my whole class looks like below.





































Nothing new here, If you have already gone throw my 

I have written an observer on salary property and observing the changes in the same class and changing the value of the isTaxApplicable property based on the salary value.

Here, one thing I want to discuss is the options parameter in addObserver and change parameter in the observeValueForKeyPath method.

NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld

I want both the values here. I mean old value of the salary as well as new salary.




                         


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

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