I have three classes that share the same instance of an NSObject
subclass.
All those three classes can modify that shared instance attributes and, when that happen, that shared instance must notify all three other classes that X
variable has changed.
This is the perfect scenario to use KVO
, but I think that it is very inconsistent code with a lot of hardcoded strings, thus no compile time warning or checking occurs, it is difficult to get stacktrace when something goes wrong, in case of refactoring code it is a headache to change everything and pray to not forget anything, remove observers manually when deallocating, repeat a lot of code by adding same observers to all classes etc.
Due to that reasons, I thought that delegates
may be a good solution to the majority (if not all) of those problems. Easy to implement, very clear and documented, if something change the compiler tells you about it, no need to manually set to nil anything, no need to have hardcoded strings, obvious stracktrace when something goes wrong etc.
My delegate
approach would be overriding all variables' setters
that I want to be listening to their changes and call the delegate right there so that all classes can know about that change.
What do you think about this approach?
Thank you in advance!