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();
}
Here's a tip from Marcus Zarra (author of Core Data book):
Core Data error when deleting row in tableView
"Try breaking on objc_exception_throw and see what method is throwing the exception. That should help track it down"
Errors like this on a Core Data save when using the iPhone SDK generally point at an error in the NSFetchedResultsController delegate methods. Specifically one of the example pieces of code from Apple is incorrect and frequently produces this error. I would suggest looking at your delegate methods and compare them with the latest example code as you may find that Apple has updated the example code in the documentation and if you re-copy their code this error can go away.
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With