Re: Why is it wrong to have relationships without an inverse in Core Data?
Re: Why is it wrong to have relationships without an inverse in Core Data?
- Subject: Re: Why is it wrong to have relationships without an inverse in Core Data?
- From: Graham Cox <email@hidden>
- Date: Tue, 25 Jun 2013 11:01:55 +1000
On 25/06/2013, at 12:49 AM, Flavio Donadio <email@hidden> wrote:
> Rick Mann, I don't understand why you need a separate relationship to retrieve the active Child objects. Please, consider doing this:
>
> Parent
> children to-many to Child
>
> Child
> parent to-one to Parent
> active (boolean)
>
> And then create an activeChildren: method in Parent with returns only the active children.
>
> Sorry if this is not acceptable. I am just trying to help.
In my experience, this is not a good design pattern.
It's common to want to maintain a subset of some collection (and it may be that the subset can have at most a single member, as in Rick's case). Using a boolean as a state variable in the objects to indicate membership of this set is poor design because it a) requires a lot of management to turn off those that become deselected, and b) requires much more time to iterate over the collection to find the subset when needed, c) requires additional work if there can be multiple, orthogonal sets of this nature.
Using the language of sets it becomes clear how to do this much more efficiently: use a set! The NSSet class, and its cousin NSIndexSet, are ideal for this purpose. If it's in the set it's a member, otherwise not. It's fast and efficient, simple to extend without the member objects ever having to care about it. Sometimes the member object will want to ask "am I a member of such-and-such a set?", and this is where the child->parent relationship can be used.
This is independent of Core Data, I'm not sure what features it has to support this concept - maybe none, as typically this sort of subset (selection) management is handled at the controller level.
I commend it to the house.
--Graham
_______________________________________________
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