Note that there are some explanatory texts on larger screens.

plurals
  1. POcore data retrieving data from sets with many to many relationships
    text
    copied!<p>I am new to core data and am still trying to get my head around it. I have a problem that I have spent hours on trying to solve. I just can't seem to solve it and was hoping someone could help me.</p> <p>I can't post images yet so here is a link to my data model:</p> <p><a href="http://yourpcsolution.net/model.png" rel="nofollow">http://yourpcsolution.net/model.png</a></p> <p>Each relationship has an inverse. A Game can have many Periods and a Period can have many Players. A Player can be in more than one Period.</p> <p>Here is my code for populating DB:</p> <pre><code>- (void) prepareDB { Games *game = [NSEntityDescription insertNewObjectForEntityForName:@"Games" inManagedObjectContext:self.managedObjectContext]; game.date = [NSDate date]; game.name = @"Game 1"; Periods *period = [NSEntityDescription insertNewObjectForEntityForName:@"Periods" inManagedObjectContext:self.managedObjectContext]; period.date = [NSDate date]; period.name = @"1st Period"; NSMutableSet *gameSet = [[NSMutableSet alloc] initWithObjects:period, nil]; game.periods = gameSet; Players *player = [NSEntityDescription insertNewObjectForEntityForName:@"Players" inManagedObjectContext:self.managedObjectContext]; player.name = @"Player1"; NSMutableSet *playerSet = [[NSMutableSet alloc] initWithObjects:player, nil]; period.players = playerSet; NSError *error; if (![self.managedObjectContext save:&amp;error]) { NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]); } } </code></pre> <p>I think that's the correct way to add data because when looking at the sql db the data is there. My problem is in trying to retrieve the players that belong to each period. Because Periods is a set in Games and Players is a set in Periods in core data. So I can't retrieve players by going: game.periods.players</p> <p>Here is my code for retrieving a Period and in the xcode log i am getting a fault:</p> <pre><code>"Relationship 'players' fault on managed object (0x8e72bc0) &lt;Periods: 0x8e72bc0&gt; (entity: Periods; id: 0x8e727c0 &lt;x-coredata://7F63902B-FCB6-4ACA-BB40-904755D37A4A/Periods/p1&gt;; data: {\n date = \"2013-07-09 19:35:48 +0000\";\n games = \"0x8e6d760 &lt;x-coredata://7F63902B-FCB6-4ACA-BB40-904755D37A4A/Games/p1&gt;\";\n name = \"1st Period\";\n players = \"&lt;relationship fault: 0x9840330 'players'&gt;\";\n})" </code></pre> <p>My Code for retrieving a Period:</p> <pre><code>NSString *firstPeriod = @"1st Period"; NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", firstPeriod]; [request setEntity:[NSEntityDescription entityForName:@"Periods" inManagedObjectContext:self.managedObjectContext]]; [request setPredicate:predicate]; NSArray *period = [self.managedObjectContext executeFetchRequest:request error:&amp;error]; </code></pre> <p>I'm not sure how to proceed from here to retrieve the players. I know how to retrieve what Periods belong to each game but I don't know how to retrieve the players that belong to a period. I have been trying for days to figure this out and it's just not working.</p> <p>Is my data model correct? How should I be retrieving the data? Any help or advice is appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload