I'm having some trouble with core data / NSFetchedResultsController. I'm not entirely sure where the error is as the message is quite vague.
I have an issue with inserting more than one object when the fetched results controller has no objects fetched. The following code will crash with the following error if I try and insert several objects with none fetched already. It doesn't crash if I use it to insert one object, and it doesn't crash if there are already objects fetched.
The crash occurs on the save: method. titles in an NSArray and in this example it contains 5 strings.
Serious application error. Exception was caught during Core Data change processing: * -[NSCFArray objectAtIndex:]: index (4) beyond bounds (1) with userInfo (null) * Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (4) beyond bounds (1)'
NSEnumerator *titleEnumerator = [titles objectEnumerator];
NSString *title;
NSMutableArray *tasks = [NSMutableArray array];
Todo *todo;
while(title = [titleEnumerator nextObject])
{
todo = (Todo *)[NSEntityDescription insertNewObjectForEntityForName:@"Todo" inManagedObjectContext:managedObjectContext];
todo.title = title;
todo.state = [NSNumber numberWithInteger:TodoStateIncomplete];
todo.priority = [NSNumber numberWithInteger:TodoPriorityNormal];
todo.timeStamp = [NSDate date];
todo.dueDate = [NSDate distantFuture];
}
NSError *error;
if(![managedObjectContext save:&error])
{
NSLog(@"Unresolved error %@ %@", error, [error userInfo]);
abort();
}