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。
任何人都有這個工作權利?