3

I have an app using tableview and NSFetchedResultsController. I am getting the error:

Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.   Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 1 in section at index 0'

... from the code below. selectedevents is an array whose count is equal to that of objects in fetched results controller.

NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSMutableArray *array=[[NSMutableArray alloc] init];
for (int i=0; i<[self.selectedEvents count]; i++) {
    CustomDictionary *dic=[selectedEvents objectAtIndex:i];
    if (dic.isSelected) {
        Event *evt=[fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        [array addObject:evt];
    }
}
for (Event *evt in array) {
    [context deleteObject:evt];
}
NSError *error;
if (![context save:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
TechZen
  • 64,370
  • 15
  • 118
  • 145
Abhinav Batra
  • 517
  • 2
  • 7
  • 12

2 Answers2

2

in the code below "selectedevents" is an array whose count is equal to that of objects in fetched results controller.

but

error says that 'no object at index 1 in section at index 0' your fetched result controller may have multiple sections.

Also, code should be:

NSManagedObject *evt=[fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];

...not:

Event *evt=[fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
TechZen
  • 64,370
  • 15
  • 118
  • 145
Adarsh V C
  • 2,314
  • 1
  • 20
  • 37
2

Attempting to base another array on [NSFetchedResultController fetchedObjects] is dangerous because the fetched results controller is always automatically updating itself. Your problem is caused by this loop:

   for (int i=0; i<[self.selectedEvents count]; i++) { 
        CustomDictionary *dic=[selectedEvents objectAtIndex:i];
        if (dic.isSelected) {
            Event *evt=[fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
            [array addObject:evt];
        }
    }

... looping twice when you only have one object returnable by [NSFetchedResultController objectAtIndexPath:]. That is probably caused by selectedEvents being set when fetchedObjects has two or more elements and then one is deleted without updating selectedEvents for the new count. When you try to loop over the rows, you go at least one to far and get the exception.

Changing the for-loop to index directly on fetchedObjects:

for (int i=0; i<[[fetchedResultsController fetchedObjects] count]; i++)

... will solve the immediate problem but your code will always be fragile if you rely on trying to sync the selectedEvents array with the fetched results controller.

TechZen
  • 64,370
  • 15
  • 118
  • 145