Note that there are some explanatory texts on larger screens.

plurals
  1. POCore Data Relationships fetch
    primarykey
    data
    text
    <p>Ok, I thought I had this but I am not getting the results that I am expecting. Hopefully someone can help.</p> <p>I have two entities Person and Timesheet with one attribute to-many relationship: Person.timesheet&lt;--->>Timesheet.user. The code below works but when I try to add a second timesheet entry it seems to override the first?</p> <p>I have looked at the Apple Docs and they are a little vague on this subject.</p> <pre><code>//Add NSManagedObjectContext *context = self.managedObjectContext; Person *personAdded = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context]; Timesheet *timesheet = [NSEntityDescription insertNewObjectForEntityForName:@"Timesheet" inManagedObjectContext:context];; timesheet.time = @"10:00 Friday"; timesheet.timestamp = [NSDate date]; NSSet *timesheetSet = [NSSet setWithObject:timesheet]; personAdded.name = @"Darren"; personAdded.job = @"Job to be Done"; personAdded.timesheet = timesheetSet; NSError *error = nil; [context save:&amp;error]; if (error) { NSLog(@"[ERROR] COREDATA: Save raised an error - '%@'", [error description]); } NSLog(@"[SUCCESS] COREDATA: Inserted new User to database!"); // Load NSEntityDescription *personEntity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity: personEntity]; error = nil; NSArray *results = [context executeFetchRequest:request error:&amp;error]; if (!results || error) { NSLog(@"[ERROR] COREDATA: Fetch request raised an error - '%@'", [error description]); [request release]; } NSLog(@"Results: %@",results); Person *firstUser = [results objectAtIndex:0]; NSLog(@"First User's name: %@",firstUser.name); NSLog(@"First User's time %@",[[firstUser.timesheet anyObject] valueForKeyPath:@"timestamp"]); </code></pre> <p>I am wondering if it could be because I am actually setting the Person.timesheet key with the NSSet and not the actual Table? OR could it be that I am not calling the results correctly?</p> <p>Thanks, Darren</p>
    singulars
    1. This table or related slice is empty.
    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.
    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