Ios NSFetchedResultsController委托仅在第一次启动应用程序时激发方法
对不起,我的英语很糟糕。 我的iOS应用程序有大问题。应用程序拥有由核心数据管理的大型数据库。我有许多用于显示这些数据的TableView控制器。数据库中的任何更改都应显示在tableview中。可以通过实现NSFetchedResultsController委托协议来实现。所有的实现都很简单,就像在书中一样。如果应用程序第一次在模拟器中启动,并且我在一些表中添加了新条目,则成功触发下一个委托方法:Ios NSFetchedResultsController委托仅在第一次启动应用程序时激发方法,ios,core-data,nsfetchedresultscontroller,Ios,Core Data,Nsfetchedresultscontroller,对不起,我的英语很糟糕。 我的iOS应用程序有大问题。应用程序拥有由核心数据管理的大型数据库。我有许多用于显示这些数据的TableView控制器。数据库中的任何更改都应显示在tableview中。可以通过实现NSFetchedResultsController委托协议来实现。所有的实现都很简单,就像在书中一样。如果应用程序第一次在模拟器中启动,并且我在一些表中添加了新条目,则成功触发下一个委托方法: – controllerWillChangeContent: – controller:didC
– controllerWillChangeContent:
– controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:
– controllerDidChangeContent:
停止调试并再次启动应用程序后,不会触发列出的任何方法。
它们仅在执行[managedObjectContext保存]
操作后调用
你知道为什么会这样吗
源代码:
//IssueProfileViewController.h class implements NSFetchedResultController delegate methods
- (NSFetchedResultsController*)fetchedResultsController{
if (_fetchedResultsController == nil) {
NSManagedObjectContext *managedObjectContext = self.issue.managedObjectContext;
NSFetchRequest *aFetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"IssueHistoryItem" inManagedObjectContext:managedObjectContext];
[aFetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
//NSPredicate *predicate = [[NSPredicate predicateWithFormat:@"issue == %@ && isComment == NO", self.issue] retain];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"issue == %@ && isComment == NO", self.issue];
[aFetchRequest setSortDescriptors:sortDescriptors];
[aFetchRequest setPredicate:predicate];
NSFetchedResultsController *aFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:aFetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[aFetchRequest release];
//[predicate release];
[sortDescriptors release];
[sortDescriptor release];
_fetchedResultsController = aFetchedResultsController;
_fetchedResultsController.delegate = self;
}
return _fetchedResultsController;
}
-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
-(void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
NSArray *paths;
// NSIndexSet *section = [NSIndexSet indexSetWithIndex:[newIndexPath section]];
NSIndexPath *cellContentIndexPath;
switch (type) {
case NSFetchedResultsChangeInsert:
// paths = [NSArray arrayWithObject:newIndexPath];
if (![anObject isKindOfClass:[ChangeIssueDimensionValueHistoryItem class]]) {
cellContentIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row inSection:newIndexPath.section];
paths = [NSArray arrayWithObject:cellContentIndexPath];
[self.tableView insertRowsAtIndexPaths:paths
withRowAnimation:UITableViewRowAnimationFade];
[self sendMessageAboutObjectsCountChanged];
}
break;
case NSFetchedResultsChangeDelete:
paths = [NSArray arrayWithObject:indexPath];
[self.tableView deleteRowsAtIndexPaths:paths
withRowAnimation:UITableViewRowAnimationFade];
[self sendMessageAboutObjectsCountChanged];
break;
case NSFetchedResultsChangeMove:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[self.tableView cellForRowAtIndexPath:indexPath]
withIssueHistoryItem:[self.fetchedResultsController objectAtIndexPath:indexPath]];
break;
default:
break;
}
}
这似乎是NSFetchedResultsController执行的正确方式——响应模型层的更改(即
[managedObjectContext save]
)
在文档中:在“响应更改”下,说明在托管对象的上下文收到
processPendingChanges
消息之前,控制器不会显示更改。该消息也可以手动触发。谢谢您的回答!但如果我理解正确,当我向上下文添加新实体时,它必须触发processPendingChanges
?是否?是的,如果NSFetchedResultsController的NSManagedObjectContext接收到processPendingChanges,则tableView应该更新。并且(在这里重申)在保存上下文时会自动发生这种情况。