开发者

Fetching Core Data for Tableview on iPhone - Tutorial leaves me with crashes when adding items to a list

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]


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜