letterus added on 2023-08-31 19:38:50:
While KVC is implemented, KVO is currently missing.
User Story: As an application developer I'd like to port a library like Mantle or RestKit to ObjFW to easily consume RESTful APIs. They rely on NSCoding and KVO heavily.
Reference: Apple Doc
js added on 2025-04-11 19:57:20:
I think this is a feature that makes sense. Manual KVO should be fairly easy to implement, however, automatic KVO might be a bit trickier for several reasons:
- There is no central hook which is called on all property setters.
- We cannot just hack it into
objc_setProperty either, as that would not work with the Apple runtime.
- This leaves swizzling, but method swizzling is per class, not per object, meaning:
- We could either swizzle the method on the class as soon a single instance has an observer.
- This would mean we would need to keep track if there are any other instances where the same property is being watched, meaning we need to use a lock for registering an observer. Though method swizzling would involve a lock in the ObjC runtime anyway.
- We'd also need to keep track of the last observer for a property being removed to restore the original method.
- This could break property setters that call into super if the property is observed on an instance of the class and also on an instance of the superclass.
- This is probably not a problem because automatic KVO would only be used on synthesized property setters.
- Create a subclass with overridden setters for the observed object and class swizzle the object
- This obviously has a lot of overhead.
- It is probably prohibitively expensive, so things such as adding an observer for all objects in an array could quickly create severe performance and memory problems.
- There could be some smart tracking to not create a new class if an identical one already exists. Though as soon as another property is being observed, a new class needs to be created again then. This overall has quite some tracking overhead, but would significantly reduce the resource problem.
- It does not have the problem of setters calling into super.
- I have not yet made up my mind which of those two is less terrible.
Overall, there is no solution that is clearly superior, so I'll have to give it some more thought.
|