Re: Core Data performance advice... creating relationships.
Re: Core Data performance advice... creating relationships.
- Subject: Re: Core Data performance advice... creating relationships.
- From: "I. Savant" <email@hidden>
- Date: Mon, 14 Jan 2008 17:04:37 -0500
> My algorithm for creating these relationships is to fetch every entry
> in 'Foo', and enumerate through the resulting array building a fetch
> request for 'Bar' in the form of 'All entities in Bar where ID == x'.
> Then when I get that result, I set 'Foo.rel' to the NSArray returned
> by that fetch request.
If I understand your post correctly, it looks like you're trying to
force Core Data into your RDB world and it's not intended for that.
Core Data is *not* a relational database. It's an object graph
management and persistence framework. If this is the case, you're
making things way harder than they actually are. I suggest going
through the Core Data Programming Guide, but in a nutshell:
If you want to get all the Bars for Foo:
id foo = //... assume you have a reference to one instance of foo
NSSet * fooBars = [foo valueForKey:@"bars"];
... now you have a set of bars - only the bars that belong to foo
(via it's to-many "bars" relationship key).
If you want to associate a bunch of bars to a Foo instance:
id foo = // ... again, assume an instance of Foo
NSArray * bars = //... assume an array of Bar instances
[[foo mutableSetValueForKey:@"bars"] addObjectsFromArray:bars];
... now foo's bars contain what was in the "bars" array. If you ask
each individual Bar instance in that array what its "foo" is, they'll
all tell you it's your "foo" instance (the one to which you added
them).
Coming from the other direction, if you set an individual bar's foo
relationship (since it's to-one):
id foo // ... same as before
id bar // ... some bar instance
[bar setValue:foo forKey:@"foo"];
... now if you ask "bar" for its "foo", you get the foo you
specified above. Bonus: If you ask the bar's foo who all its bars are,
you'll get a set including the bar you specified above.
I hope this is clear and not fubar'd. ;-)
--
I.S.
_______________________________________________
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