NSFetchedResultsController不会在reloadData上更改

Sla*_*ter 2 core-data objective-c nsfetchedresultscontroller ios

我有一个主 - 详细信息表单的iOS应用程序,用户发布的帖子显示在主视图中,详细信息显示在评论中.当我选择一个帖子时,我可以看到所有评论都很好,但是当我回去选择另一个帖子时,数据不会改变为下一篇文章的评论.

我打电话给你 viewDidLoad

 self.fetchedResultsController = nil;
 [self.tableView reloadData];
Run Code Online (Sandbox Code Playgroud)

但没有任何反应.

这是我的代码 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;
}
Run Code Online (Sandbox Code Playgroud)

Aud*_*rup 6

你的fetched-results-controller不刷新的原因是你设置了一个缓存.您需要传递nil缓存名称,或者在刷新FRC之前删除缓存:

  [NSFetchedResultsController deleteCacheWithName:@"Master"]
Run Code Online (Sandbox Code Playgroud)