Re: The problem with bindings
Re: The problem with bindings
- Subject: Re: The problem with bindings
- From: Glenn Andreas <email@hidden>
- Date: Thu, 29 Jul 2004 16:28:44 -0500
At 4:03 PM -0400 7/29/04, Gwynne wrote:
Something as simple as this doesn't work like you'd expect:
+ (void)initialize
{
[self setKeys:[NSArray arrayWithObject:@"key1"]
triggerChangeNotificationsForDependentKey:@"key2"];
[self setKeys:[NSArray arrayWithObject:@"key2"]
triggerChangeNotificationsForDependentKey:@"key3"];
}
Now [someObject setValue:anyValue forKey:@"key1"]. key2 will be
updated, but no change notification for key3. Also, you can't do
this:
Personally, I wouldn't have expected it to - after all, when key1
changes, that doesn't mean that key2 changes, just that a
notification is triggered. If there was an automatic "cascade" of
notifications, there could easily be nasty loops with unexpected
behavior (say you added that key3 triggers key1, and you change key1,
does key1 also get the notification because it passed through
key2->key3->key1?).
I'm quite happy with simple explicit notification changes:
[self setKeys:[NSArray arrayWithObject:@"key1",@"key2"]
triggerChangeNotificationsForDependentKey:@"key3"];
(since that's clear that key1 & key2 both trigger key3)
[self setKeys:[NSArray arrayWithObject:@"singleKey"]
triggerChangeNotificationsForDependentKeys:[NSArray
arrayWithObjects:...]];
Or in English, you can't easily make multiple keys dependent on a
single key; you have to call [self
setKeys:triggerChangeNotificationsForDependentKey:] with the same
array over and over.
If you find yourself with a datamodel that has this a lot, it's
trivial to add (typed in mail):
@interface NSObject(MultipleTriggeredKeys)
- (void) setKeys: (NSArray *)keys
triggerChangeNotificationsForDependentKeys: (NSArray *)dependants
@end
@imeplentation NSObject(MultipleTriggeredKeys)
- (void) setKeys: (NSArray *)keys
triggerChangeNotificationsForDependentKeys: (NSArray *)dependants
{
NSEnumerator *dependantEnum = [dependants objectEnumerator];
id dependant;
while ((dependant = [dependantEnum nextObject]) != NULL) {
[self setKeys: keys
triggerChangeNotificationsForDependentKey: dependant];
}
}
@end
And "Bob's your uncle" (as they say).
Adopting bindings correctly requires a Major change in how you think
of and structure your model, and if you're not careful you can end
up making your code more complicated.
This is very true - it is usually _much_ easier if you use bindings
from the very start rather than try to add them to an existing
project. But at this point of having used them, I'd hate to have to
go back...(unfortunately, "let's set up a binding to do this" seems
to be my current first solution for any problem, regardless if it is
appropriate or not).
--
Glenn Andreas email@hidden
mondo blobbo, Cythera, Theldrow, oh my!
Mad, Bad, and Dangerous to Know
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.