Why does the Apple KVO implementation use subclasses instead of swizzling?

At Apple 's Key Validation Details Document , he says the implementation subclasses for forwarding setter labels. A subclass replaces the original class.

Why not just use method_exchangeImplementations()swizzle methods? Why is it necessary to create a subclass?

+4
source share
1 answer

Because when you swizzle a method, you change the method for all instances of the class. A KVO notification setter is more expensive than a non-KVO notification setter, so you want to use a KVO notification setter for instances that are actually being observed, and not for all instances of the class.

What should the KVO property setting method do? At a minimum, he should check if any KVO observers are registered for this facility using [self observationInfo]. So a guaranteed additional message is sent. In addition, it observationInfois stored in the global global hash table by default , so a KVO-compatible setter must obtain a lock and perform a hash search - only to find out if registered KVO observers are registered.

setter, , .

Apple KVO KVO. KVO , KVO-. , , , KVO- .

+8

Source: https://habr.com/ru/post/1693162/


All Articles