Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KVO firing when value assigned is not different

If I have a property as follows

@property(assign, nonatomic) NSUInteger myValue;

And assign a property to it

self.myValue = 2;

KVO will fire as expected. If, later on, I then assign the same value to it

self.myValue = 2;

KVO will fire again. I had presumed that KVO in Objective-C would not fire if the value being assigned was not different. It appears that I am incorrect.

Is there a way to force this default behaviour, i.e. disable the KVO notifications firing every time a value is assigned? I can create my own accessors, but this could be quite a lot of work if there are a lot of properties that I want to change...

Thanks for any replies.

like image 797
Niall Mccormack Avatar asked Jan 21 '13 21:01

Niall Mccormack


People also ask

How does KVO work?

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 Key-Value Coding and Key-Value observing?

KVO and KVC or Key-Value Observing and Key-Value Coding are mechanisms originally built and provided by Objective-C that allows us to locate and interact with the underlying properties of a class that inherits NSObject at runtime.

What is KVO in Objective-C?

KVO allows you to register as an observer of a given object and receive notification when specific properties on that object are changed. It's an incredibly powerful capability, and it is built into Objective-C at its very core.

What is Key-Value Coding swift?

Key-value observing is a Cocoa programming pattern you use to notify objects about changes to properties of other objects. It's useful for communicating changes between logically separated parts of your app—such as between models and views. You can only use key-value observing with classes that inherit from NSObject .


1 Answers

KVO fires when a setter (or other mutator) is called. There is no additional check. In general, KVO is extremely lightweight and performance sensitive. Since unnecessary changes are typically rare and in most cases harmless to the observer, they do not include the extra overhead of checking the old value. Checking the previous value could be expensive, for instance if a managed object hadn't been faulted, so doing a lot of work just to check for this case is not a default behavior.

If you need to check how the value changed or didn't, you can do so as the observer by passing the options NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld. You will then receive both the new and old values and you can decide whether to do anything with that information. I've found in practice that this shouldn't be needed very often, however.

like image 87
Rob Napier Avatar answered Sep 23 '22 16:09

Rob Napier