2

I cannot seem to figure out the last little bit on how to access layers of a to-many relation in Core Data (DB newbie). All examples are simple 2 entity relation. My Core Data looks like this:

Entity_A
Attribute_AA
Attribute_AB
Relation_AB (to-many from entity_A to entity_B)

Entity_B
Attribute_BA
Attribute_BB
Relation_BA (many-to from entity_B to entity_A)
Relation_BC (to-many from entity_B to entity_C)

Entity_C
Attribute_CA
Attribute_CB
Relation_CB (many-to from entity_C to entity_B)

I've successfully managed to populate the entities and fetch Entity_A into self.entity_A. The self.entity_A.relation_AB shows as faults but I've learnt that's normal and I can NSLog the values in entity_B and entity_C as NSSets using the following statements:
NSLog(@"Entity_B: %@", [self.entity_S.relation_AB valueForKey:@"Attribute_BA"]);
NSLog(@"Entity_C: %@", [[self.entity_A.relation_AB mutableSetValueForKey:@"Relation_BC"] valueForKey:@"Attribute_CA"]);

which gives me the following output that is the correct values stored in the entities in my db:
Entity_B: {(directory1, directory2)}
Entity_C: {(file1, file2, file3)},{(file4, file5, file6, file7)}
(the directory1/2 and file1/2/3/4/5/6/7 are just values stored in the respective entity in the db for example sake)

It seems the lists from NSSets are not linked so how do you know which file in entity_C is with what directory in entity_B is with what thing in entity_A?

Another way of asking is how do I get a "self.entity_A.entity_B.directory1" (this syntax may not be correct) that contains [file1, file2, file3] from the example above and a "self.entity_A.entity_B.directory2" that contains [file4, file5, file6, file7]?

Thanks for your help.
Hiren.

HM1
  • 1,684
  • 2
  • 18
  • 25
  • In your NSManagedObject, to-many relationships are accessed as NSSet instances. Take a look at this answer: http://stackoverflow.com/questions/8603224/how-to-represent-foreign-key-relationships-in-core-data-data-model-in-xcode/8652407#8652407, it'll give you some good background info on CoreData relationships in general. I didn't quite understand where your file and directory attributes were coming from. What is it you are trying to access exactly? – MGA Feb 14 '12 at 05:38
  • @Mattieu, I'm aware they're NSSet instances and have successfully outputted the values using the above nslog statements. I'm trying to get a list of files from entity_C that are related to the same directory in entity_b that related to the same thing in entity_a. (Sorry for the poor example, the file and directory are just values stored under entity b and c... perhaps i should have used fruits and vegies.) – HM1 Feb 14 '12 at 06:50
  • Can you simply iterate through the NSSet then? – MGA Feb 14 '12 at 13:47

1 Answers1

0

Just write a method to build an NSMutableDictionary by iterating through the values of Entity_B, setting them as keys, and setting the values the NSSets of corresponding values of Entity_C.

yuji
  • 16,695
  • 4
  • 63
  • 64