0
的更改我对核心数据的常规使用模式非常熟悉,但最近偶然发现了一个问题:想象一下,我有一个人的实体,有2个字符串属性名称和公司的简单案例。我想让UITableView按名称排序并按公司名称划分。听起来很简单:核心数据:对
...
personFetchController_ = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:mainDataCenter.managedObjectContext
sectionNameKeyPath:@"company"
cacheName:@"PersonListCache"];
NSError *error = nil;
if (![personFetchController_ performFetch:&error])
{
LogError(@"Error fetching persons: %@", [error localizedDescription]);
}
personFetchController_.delegate = self;
我注册不如授人以听变化,特别是更改为部分:
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
NSIndexSet *sections = [NSIndexSet indexSetWithIndex:sectionIndex];
switch (type)
{
case NSFetchedResultsChangeInsert:
[personTableView_ insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[personTableView_ deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
default:
break;
}
}
它非常好,当我添加/删除/改变一个人的名字,但如果我更改了一个人的公司名称(意思是从一个部分移动到另一个部分),应用程序崩溃,在插入后说,部分中的行数需要是旧值加1。
任何人都有这个工作权利?