开发者

NSFetchedResultsController - My _sectionNameKeyPath_ is _nil_ !! Why am I getting this error when adding / deleting?

Classic case of the sections issue with NSFetchedResultsController and I am pulling my hair out. I have set the sectionNameKeyPath to NIL which I've read is what you want for no sections (i.e 1 section).

Code as follows:

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSF开发者_JAVA百科etchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription 
                                   entityForName:@"MyObjectType" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
                              initWithKey:@"name" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"accountExpenseTypeCache"];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    [sort release];
    [fetchRequest release];
    [theFetchedResultsController release];


    return _fetchedResultsController;  
    }

As you can see my sectionNameKeyPath is set to nil and

Sometimes when I add or delete a row, it works. Most often than not I get this serious crash in the console though when trying to delete or add:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:955
(gdb) continue
2011-03-14 18:00:58.104 MyApplicationTest[5741:207] Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (3) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted). with userInfo (null)

What's the issue? I was reading the NSFetchedResultsController prior to 4.0 was built to handle User Driven updates? I can only assume adding and deleting rows are considered User Driven updates? Do I need to implement something like this?

How to implement re-ordering of CoreData records?

?? From what I've read though I don't understand why I have this issue when I have nil for sections. Why is it saying that are 3 sections?!

Thanks in advance!


The number of sections in the tableview is not dependent on the sectionNameKeyPath of the fetched results controller but rather by the returned by the datasource's numberOfSectionsInTableView:.

The error you are getting can be triggered by not sending the tableview a beginUpdate message prior to changing the data. The tableview may try to redraw itself while the count of the data is in flux.

You must also implement the fetched results controller's delegate methods in the tableview's datasource object so that table is signaled when a change occurs.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜