Question about model observers
Question about model observers
- Subject: Question about model observers
- From: Eric Giguere <email@hidden>
- Date: Tue, 11 May 2010 10:10:35 -0400
Hi all
I have to deal here with a pretty usual situation where my entity has a to-many relationship to another entity. I need, in my parent entity, to be notified when children are added or removed since I have another attribute in my entity and its value depends on this relationship's content.
I read a lot about it, and finally managed to have my custom accessor methods working by doing a workaround on a bug in the framework that prevents these custom functions to be called when the add/ remove are done on an Array controller of the children bound to my parent Array controller using the selection controller key.
> Cause: This is a bug.
>Remedy: You can work around this by adding self to the contentSet binding's key path. For example, instead of binding to [Department >Object Controller].selection.employees, you would bind to [Department Object Controller].selection.self.employees.
That finally worked!!! The accessors are called and I can set now my internal "dirty flag" to have my code kicking in when my dependant attribute is called (a string resume of the list's content)
Everybody tends to point in using the KVO notifications instead of the custom accessors, even if those are generated. Ok, but I couldn't make it work. Using controllers, no problem but what if I want my parent entity to be its own observer? After all, I am talking here about a relationship where children are "owned" by its parent. The behaviour on my parent when child list is modifies shouldn't require to go out of the model and use controllers. So I tried creating an observer this way, of course, didn't work:
[self addObserver:self forKeyPath:@"needIngredients" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
Up there, the needIngredients is my many relationship property.
I Apple's documentation, I also read about setting the parent as a listener to an attribute of each child in the list! Does not make sense at all. And anyway, if the first place if you want to add / remove self as observer in each of your children, you must supply custom accessor methods to make that association don't we? That kinds of go back to square 1 and define custom accessor methods.
So the real question here:
What is the "right" method to have those add / remove notification in the model? Custom accessors, KVO?
If KVO should be the way, then what would be the right approach using only model entities to have this working? The controller layer should never be involved in that kind of logic.
thx a lot!
Eric
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden