On Fri, Nov 16, 2012, at 02:00 PM, Rick Mann wrote: > > On Nov 16, 2012, at 2:31 , Mike Abdullah <cocoa...@mikeabdullah.net> > wrote: > > > Why are you trying to avoid "a bunch of" these calls? Is it just to save > > yourself typing? > > Typing, and code maintenance. I generally have UI that displays all (or > some subset of) the properties of one of my entities. I'd like that UI to > update if anything updates one of the models. But if there are a dozen > properties, then that's a dozen -addObserver and -removeObserver calls, > in each place where there might be UI associated with it. If I later add > a new property to the entity, I have to be sure to KVO, it, too. If I > remove a bit of the UI, I have to remember to remove the call (not > strictly, but I like keeping my code clean). > > Being able to subscribe once for all changes to a single object makes > these problems go away.
FWIW, in an app I worked on that does not use Core Data but otherwise has model objects with a dozen or so properties that are interesting to a single observer, I wrote a few handy macros to walk over a static C array of keypaths: https://github.com/omnigroup/OmniGroup/blob/master/Frameworks/OmniBase/OBUtilities.h#L247 Then when I start observing a new model object, I can do something like this: static NSString interestingKeypaths[] = {@"keyPathOne", @"keyPathTwo"}; static void *ctx = &ctx; - (void)_startObservingModelObject:(NSObject *obj) { OB_FOR_IN(keyPath, interestingKeyPaths) [obj addObserver:self forKeyPath:keyPath options:0 context:ctx]; } - (void)_stopObservingModelObject:(NSObject *obj) { OB_FOR_IN(keyPath, interestingKeyPaths) [obj removeObserver:self context:ctx]; } --Kyle Sluder _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com