Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is Core Data losing one of my values?
    primarykey
    data
    text
    <p>Inside my user object I have the following code to generate a new 'session' or continue the existing session if one exists.</p> <p>Strangely it will keep other properties but just loses the 'user' property... user is in a one to many relationship with session, 1 user can have many sessions. (or will do, for the following test I am simply checking for any previous session and using it if it exists)</p> <pre><code>-(void)setupSessionStuff { // Create new Core Data request NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Session" inManagedObjectContext:[self managedObjectContext]]; [request setEntity:entity]; // Create Sort Descriptors for request NSSortDescriptor *startTimeSort = [[NSSortDescriptor alloc] initWithKey:@"startTime" ascending:NO selector:nil]; [request setSortDescriptors:[NSArray arrayWithObjects:startTimeSort, nil]]; [startTimeSort release]; [request setFetchLimit:1]; // Only get the most recent session // Execute request NSError *error = nil; NSArray *results = [[self managedObjectContext] executeFetchRequest:request error:&amp;error]; if (results == nil) { // Something went horribly wrong... NSLog(@"Unresolved error %@, %@", error, [error userInfo]); exit(-1); } [request release]; Session *theSession = nil; if ([results count] == 1) { NSLog(@"existing session"); // Use existing Session theSession = [results objectAtIndex:0]; NSLog(@"session.user: %@", [theSession valueForKey:@"user"]); // this is always null! } else { NSLog(@"new session"); // Create new Sesson theSession = (Session *)[NSEntityDescription insertNewObjectForEntityForName:@"Session" inManagedObjectContext:[self managedObjectContext]]; // Add the Session to the User NSLog(@"before: session.user: %@", theSession.user); // null theSession.user = self; NSLog(@"after: session.user: %@", theSession.user); // looks good } ... NSLog(@"before.save: session.user: %@", theSession.user); // good // Save everything error = nil; if (![[self managedObjectContext] save:&amp;error]) { // Something went horribly wrong... NSLog(@"Unresolved error: %@, %@, %@", error, [error userInfo],[error localizedDescription]); exit(-1); } NSLog(@"after.save: session.user: %@", theSession.user); // still there.. } </code></pre> <p>Additionally I have opened up the Core Data sqlite file and examined with SQLite Manager. It <em>looks</em> like the relationship has been correctly saved, as I can see the userID stored in the session table.</p> <p>-</p> <p>Just added this at the start of my method as another test.</p> <pre><code>NSSet *set = self.session; for(Session *sess in set) { NSLog(@"startTime %@", sess.startTime); NSLog(@"user %@", sess.user); } </code></pre> <p>Strangely enough the user is set in this case!? So set here then not set a few lines later when I do the fetch request... ?</p> <p>-</p> <p>In response to feedback below</p> <p>Have added this code after assigning <code>session.user = self</code> and both return the expected output. So it does look like the problem is with the subsequent fetch.</p> <pre><code>NSLog(@"self.session: %@", self.session); NSLog(@"self.session: %@", [self valueForKey:@"session"]); </code></pre> <p>Also I agree that accessing my session's through self.session will let me work around my issue, but it doesn't solve what is going on here. In other places I surely won't be able to walk from one entity to the other so need to confidence the fetch is going to pull everything in correctly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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