Inside my user object I have the following code to generate a new ‘session’ or continue the existing session if one exists.
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)
-(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:&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:&error]) {
// Something went horribly wrong...
NSLog(@"Unresolved error: %@, %@, %@", error, [error userInfo],[error localizedDescription]);
exit(-1);
}
NSLog(@"after.save: session.user: %@", theSession.user); // still there..
}
Additionally I have opened up the Core Data sqlite file and examined with SQLite Manager. It looks like the relationship has been correctly saved, as I can see the userID stored in the session table.
–
Just added this at the start of my method as another test.
NSSet *set = self.session;
for(Session *sess in set) {
NSLog(@"startTime %@", sess.startTime);
NSLog(@"user %@", sess.user);
}
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… ?
–
In response to feedback below
Have added this code after assigning session.user = self and both return the expected output. So it does look like the problem is with the subsequent fetch.
NSLog(@"self.session: %@", self.session);
NSLog(@"self.session: %@", [self valueForKey:@"session"]);
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.
Well I found the problem and solved my issue…
After examining the memory address of my session entity I noticing that it was changing between runs. Investigating further I discovered that where I had been testing some code earlier in another class, creating a new session entity, but not saving it, well, it was being saved after all – when I issued the save in my code above!