繁体   English   中英

NSFetchedResultsController-我的_sectionNameKeyPath_是_nil_! 为什么添加/删除时出现此错误?

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

NSFetchedResultsController是本节中的典型案例,我正在拔头发。 我已经将sectionNameKeyPath设置为NIL ,而我没有任何部分(即1个部分)想要读取的内容。

代码如下:

- (NSFetchedResultsController *)fetchedResultsController {

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

    NSFetchRequest *fetchRequest = [[NSFetchRequest 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;  
    }

如您所见,我的sectionNameKeyPath设置为nil,

有时,当我添加或删除行时,它会起作用。 通常,在尝试删除或添加时,我通常会在控制台中遇到严重崩溃:

*** 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)

怎么了 我在读取4.0之前的NSFetchedResultsController旨在处理用户驱动的更新? 我只能假定添加和删除行被视为用户驱动更新? 我需要实现这样的东西吗?

如何实现CoreData记录的重新排序?

?? 从我所读的内容中,虽然我不明白为什么我在节中没有nil时会遇到这个问题。 为什么说是三个部分?

提前致谢!

tableview中的节数不取决于获取的结果控制器的sectionNameKeyPath ,而是取决于数据源的numberOfSectionsInTableView:返回的值。

通过在更改数据之前不向tableview发送beginUpdate消息可以触发您遇到的错误。 当数据计数不断变化时,tableview可能会尝试重绘自身。

您还必须在tableview的数据源对象中实现提取的结果控制器的委托方法,以便在发生更改时向表发出信号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM