views:

256

answers:

1

Been following the Core Data tutorial on Apple's developer site, and all is good until I have to add something to the fetched store. I am getting this error after a successful build and load when I try to add a new item to the list:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted).

Due to the fact that the fetch goes through fine, and that if I replace the fetching with eventList = [[NSMutableArray alloc] init] it works as expected (without persistance, of course), I am led to believe that the problem comes from not creating the Mutable Array correctly. Here's the problematic part of the code:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];

NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults = nil) {
    //Handle the error
}

[self setEventList:mutableFetchResults];
[mutableFetchResults release];
[request release];

I have tried switching the NSArrays in the second chunk out with NSMutableArrays, but I still get the same error.

For reference, the section of code that is throwing the error when I try adding an entry is here:

[eventList insertObject:event atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

it errors out at the insertRowsAtIndexPaths call.

Thanks in advance for any help

--EDIT TO ADD--

I should note that the number of rows is being called by returning [eventList count]

+1  A: 

Well, there's a typo at if (mutableFetchResults = nil)--it should be ==, of course (which would screw everything up from that point on).

Assuming that's not the problem--keeping the Core Data information and the table array in sync can be tricky (it's hard to tell exactly what's wrong from your code, but [eventList insertObject:event atIndex:0] looks a likely culprit). If you're going to be adding and deleting objects, I'd recommend using NSFetchedResultsController instead--it has its own set of gotchas, but it makes adding and deleting objects from the store much less painful.

eman
Cannot believe I made such a stupid mistake. That fixed it. I'm going to migrate over to `NSFetchedResultsController` soon. Just trying to get some of the basics down before I move into that direction. Thanks for the help.
Gordon Fontenot