Note that there are some explanatory texts on larger screens.

plurals
  1. PODuplicate records in Core Data on fetch
    text
    copied!<p>Folks,</p> <p>I've got a bit of a puzzler involving Core Data in my iPhone project. During the viewDidLoad method in two separate view controller instances I call the following method:</p> <pre><code>- (NSFetchedResultsController *)getStudentResults:(NSString *)classRoom forWeekStarting:(NSDate *)startDate andEnding:(NSDate *)endDate { AttendanceAppDelegate *appDelegate = (AttendanceAppDelegate *)[[UIApplication sharedApplication] delegate]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:[appDelegate managedObjectContext]]; int secondsInAWeek = 60 * 60 * 24 * 7; NSDate *today = [[NSDate alloc] init]; NSDate *nextWeek = [[NSDate alloc] initWithTimeIntervalSinceNow:secondsInAWeek]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(student.classRoom like %@) AND (dateTime &gt;= %@) AND (dateTime &lt;= %@)", classRoom, startDate, endDate]; [request setPredicate:predicate]; [request setEntity:entityDescription]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dateTime" ascending:YES]; NSArray *descriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [request setSortDescriptors:descriptors]; [descriptors release]; [sortDescriptor release]; [nextWeek release]; [today release]; NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:[appDelegate managedObjectContext] sectionNameKeyPath:nil cacheName:@""]; NSError *error; if (![fetchedResultsController performFetch:&amp;error]) NSLog(@"Error performing fetch on fetchedResultsController: %@", [error localizedDescription]); if (fetchedResultsController == nil || [[fetchedResultsController fetchedObjects] count] == 0) { Student *student = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:[appDelegate managedObjectContext]]; NSData *classRoomStudentData = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"STUDENT_ATTENDANCE" ofType:@"XML"]]; [student setClassRoom:@"default"]; [student buildWithStudentData:classRoomStudentData startingWithXPathNode:@"//attendance"]; NSError *error; if (![[appDelegate managedObjectContext] save:&amp;error]) { NSLog(@"Error saving the managedObjectContext: %@", [error localizedDescription]); } if (![fetchedResultsController performFetch:&amp;error]) NSLog(@"Error performing fetch on fetchedResultsController: %@", [error localizedDescription]); } return fetchedResultsController; } </code></pre> <p>Each time I run my app the number of Student objects increases by the number of original entities (each original entity is duplicated once). So if I start with 4, the next time I get 8, then 12, then 16, etc. I can't seem to figure out why since the code to parse the XML only gets called when the fetch returns no objects.</p> <p>Any help would be appreciated, thanks.</p> <p>-- Michael</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