Skip navigation.
 
mlRe: Core Data performance advice... creating relationships.
FROM : I. Savant
DATE : Mon Jan 14 23:04:37 2008

> 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.

Related mailsAuthorDate
mlCore Data performance advice... creating relationships. Martin Linklater Jan 14, 22:43
mlRe: Core Data performance advice... creating relationships. Scott Ellsworth Jan 14, 22:56
mlRe: Core Data performance advice... creating relationships. I. Savant Jan 14, 23:04
mlRe: Core Data performance advice... creating relationships. I. Savant Jan 14, 23:12
mlRe: Core Data performance advice... creating relationships. Martin Linklater Jan 14, 23:18
mlRe: Core Data performance advice... creating relationships. Martin Linklater Jan 15, 00:10
mlRe: Core Data performance advice... creating relationships. marcelo.alves Jan 15, 02:57
mlre: Core Data performance advice... creating relationships. Ben Trumbull Jan 15, 05:22
mlRe: Core Data performance advice... creating relationships. Phil Jan 15, 10:15
mlre: Core Data performance advice... creating relationships. Martin Linklater Jan 15, 10:21
mlRe: Core Data performance advice... creating relationships. Martin Linklater Jan 15, 10:37
mlRe: Core Data performance advice... creating relationships. Chris Hanson Jan 15, 10:56
mlRe: Core Data performance advice... creating relationships. Martin Linklater Jan 15, 11:31
mlRe: Core Data performance advice... creating relationships. Phil Jan 15, 12:46
mlRe: Core Data performance advice... creating relationships. I. Savant Jan 15, 13:07
mlRe: Core Data performance advice... creating relationships. I. Savant Jan 15, 13:14
mlRe: Core Data performance advice... creating relationships. Sean McBride Jan 15, 16:31
mlRe: Core Data performance advice... creating relationships. Phil Jan 15, 17:14
mlRe: Core Data performance advice... creating relationships. mmalc crawford Jan 15, 17:21
mlRe: Core Data performance advice... creating relationships. I. Savant Jan 15, 17:22
mlRe: Core Data performance advice... creating relationships. I. Savant Jan 15, 17:26
mlRe: Core Data performance advice... creating relationships. mmalc crawford Jan 15, 17:32
mlRe: Core Data performance advice... creating relationships. Phil Jan 15, 21:57
mlre: Core Data performance advice... creating relationships. Ben Trumbull Jan 15, 23:50
mlRe: Core Data performance advice... creating relationships. Martin Linklater Jan 16, 00:49
mlRe: Core Data performance advice... creating relationships. Ben Trumbull Jan 16, 04:27
mlRe: Core Data performance advice... creating relationships. Phil Jan 16, 08:04
mlre: Core Data performance advice... creating relationships. Martin Linklater Jan 17, 18:20