我有一个Master-Detail窗体中的iOS应用程序,其中由用户创建的帖子显示在详细信息中显示的主视图和注释中。当我选择一篇文章时,我可以看到所有评论,但是当我回去选择另一篇文章时,数据不会更改为下一篇文章的评论。NSFetchedResultsController不会在reloadData上更改
我称这种现象viewDidLoad
self.fetchedResultsController = nil;
[self.tableView reloadData];
但没有任何反应。
这里是我的fetchedResultsController
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Comment" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"question == %@", self.detailItem];
[fetchRequest setPredicate:predicate];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creator" ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error in detail %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
这做到了!设置缓存为零为我工作。核心数据对我来说是新的和可怕的,但我正在慢慢获得它。谢谢! – Slayter 2013-03-19 13:53:31