我在Objective-C中有以下问题。当我从我的ManagedObjectContext中删除一个对象并保存了这个改变时,我如何通知我的fetchedResultsController执行新的提取?根据我的理解,发送performFetch-Message方法是不正确的。如何通知NSFetchedResultsController关于[NSManagedObjectContext保存]
谢谢!
我在Objective-C中有以下问题。当我从我的ManagedObjectContext中删除一个对象并保存了这个改变时,我如何通知我的fetchedResultsController执行新的提取?根据我的理解,发送performFetch-Message方法是不正确的。如何通知NSFetchedResultsController关于[NSManagedObjectContext保存]
谢谢!
您可以使用postNotificaton
为此,您应该已经在控制器中设置了一个观察器,该观察器将再次启动提取请求。使用该委托方法
你可以把它更多钞票
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:nil];
,并设置参数cacheName到零值。它避免了崩溃和ManagedObjectContext变更后只保留最新数据
#pragma mark -
#pragma mark Fetched results controller delegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableViews = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableViews insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableViews deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[_delegate configureCell:[tableViews cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableViews deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableViews insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:NO];
}
这段代码自动更新并重新加载当您添加,删除或更新NSFetchedResultsController和的UITableViewController
只要更改次数很少就可以使用。如果你得到一个大的批量更新,那么这将会严重滞后UI线程...... – jjxtra 2012-02-25 15:40:16
即使只是1,我也遇到了麻烦。但它不是滞后。 – 2012-03-19 05:04:41
postNotification的数据? – 2012-03-19 05:10:02