Search code examples
iosobjective-cperformancekey-value-observing

Disable Key-Value Observing in Objective C


I don't use KVO, mostly for performance reasons, so I would like to make sure that I disable it properly.

From Apple Key-Value Observing Programming Guide

Automatic support (for KVO) is provided by NSObject and is by default available for all properties of a class that are key-value coding compliant. Typically, if you follow standard Cocoa coding and naming conventions, you can use automatic change notifications—you don’t have to write any additional code.

Is that mean that every property generated by Xcode has willChangeValueForKey and didChangeValueForKey methods implemented?

If so is there some way (some flag or something) to disable this behaviour? I'm using accessInstanceVariablesDirectly and returning always NO, but I'm not sure if this is good enough.


Solution

  • I don't use KVO.

    You cannot really know that. Cocoa framework classes or other foreign code might depend on KVO without your knowledge.

    Also, NSObject's KVO uses a technique called isa-swizzling to automatically augment observed objects by dynamically subclassing them. That means that there's no overhead on objects that are not observed.

    Your other questions:

    accessInstanceVariablesDirectly is used in KVC only, and is not connected to KVO. There's no performance benefit in overriding it.

    Does that mean that every property generated by Xcode has willChangeValueForKey and didChangeValueForKey methods implemented?

    Yes, every object responds to these methods. But the presence of these methods again does not mean that performance is affected in any way. Usually the methods are not even called.