2

我有一个主从形式的 iOS 应用程序,用户发布的帖子显示在主视图中,评论显示在详细信息中。当我选择一个帖子时,我可以很好地看到所有评论,但是当我返回并选择另一个帖子时,数据不会更改为下一个帖子的评论。

我称之为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;
}
4

3 回答 3

6

您的 fetched-results-controller 不刷新的原因是您设置了缓存。您要么需要nil作为缓存名称传递,要么在刷新 FRC 之前删除缓存:

  [NSFetchedResultsController deleteCacheWithName:@"Master"]
于 2013-03-19T13:33:41.340 回答
2

您是否尝试过调用您的重置代码viewDidAppearviewDidLoad通常不会在视图的生命周期中多次调用。viewWillAppear/viewDidAppear当视图实际出现在屏幕上时调用。

您是否尝试过对表重新加载方法设置断点以确保它实际上被调用?

于 2013-03-19T13:07:28.040 回答
1

试试这个代码:

在调用这个方法之前不要忘记写self.fetchedResultsController = nil;

- (NSFetchedResultsController *)fetchedResultsController // attaches an NSFetchRequest to this UITableViewController
{
    if (_fetchedResultsController != nil )
    {
        return _fetchedResultsController;
    }

    NSLog(@"Length:: %d",[[_fetchedResultsController mutableCopy] length]);

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Comment"];
    [fetchRequest setIncludesSubentities:YES];

    // Sort by Application name
    NSSortDescriptor* sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"creator" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
    //Sort by CommandNameloca
    NSArray* sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor1, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    [fetchRequest setFetchBatchSize:100];

    ordersArr=[[NSMutableArray alloc]initWithArray:sortDescriptors];

    NSLog(@"----- ordersArr Count == %d",[ordersArr count]);

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"creator" 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 %@, %@", error, [error userInfo]);
        abort();
    }
    return _fetchedResultsController;
}
于 2013-03-19T06:05:06.703 回答