ios - 更改 NSFetchedResultsController 的获取请求和重新加载表数据的方法

标签 ios uitableview nsfetchedresultscontroller nsfetchrequest reloaddata

来自苹果文档 Modifying the Fetch Request我看到可以更改 NSFetchedResultsControllerNSFetchRequest。步骤很容易设置。

在调用 performFetch: 之后,我认为需要在 TableView 上调用 reloadData。如何执行这样的调用?

阅读一些 stackoverflow 主题后,我发现在大多数情况下调用该方法应该有效。但是有正确的方法吗?

How to switch UITableView's NSFetchedResultsController (or its predicate) programmatically? , TechZen 写道:

Just make sure to send the tableview itself a beginUpdates before you swap controllers and then an endUpdates when you are done. This prevents the table from asking for data in the narrow window when the FRC are being swapped out. Then call reloadData.

你能具体解释一下这是什么意思吗?

最佳答案

假设生成正确提取的逻辑(某种条件语句)在 NSFetchedResultsController 实例的 getter 中。那么就真的很简单了

self.fetchedResultsController = nil; // this destroys the old one
[self.tableview reloadData]; 
// when the table view is reloaded the fetchedResultsController will be lazily recreated

编辑:添加我所做的事情的完整代码示例。基本上我有一个 NSDictionary entityDescription,它包含用于自定义创建 NSFetchedResultsController 的值。如果我想更改 fetchRequest,我会更改 entityDescription 变量以指示新值并覆盖 setter 以重置 fetchedResultsController 并重新加载表。它为您提供了基本概念。

- (NSFetchedResultsController *)fetchedResultsController 
{
    if (__fetchedResultsController != nil) {
        return __fetchedResultsController;
    }
    if (self.entityDescription == nil) {
        return nil;
    }
    // Set up the fetched results controller.
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[self.entityDescription objectForKey:kEntityName]];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    if ([[self.entityDescription objectForKey:kEntitySortField] isEqualToString:@"null"] == NO) {
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:[self.entityDescription objectForKey:kEntitySortField] ascending:YES];
        NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
        [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.moc sectionNameKeyPath:nil cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return __fetchedResultsController;
}

- (void)setEntityDescription:(NSDictionary *)entityDescription
{
    _entityDescription = entityDescription;
    self.fetchedResultsController = nil;
    [self.tableView reloadData];
}

关于ios - 更改 NSFetchedResultsController 的获取请求和重新加载表数据的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10170997/

相关文章:

objective-c - 如何使用 CATransform3D 为一组 CALayer 添加阴影

ios - 如何从 UITableView 中删除不需要的单元格或行

ios - 何时使用 UICollectionView 而不是 UITableView?

ios - 为什么 NSFetchedResultsController 的 fetchedObjects 数组并不总是同质的

iphone - sectionNameKeyPath 通过多重关系

ios - 使用 NSFetchedResultsController 执行搜索

ios - Iphone 应用程序因侵犯版权而被拒绝

ios - 在 ViewController 中设置变量之前执行 ViewModel 函数

ios - 在 UIWebview 中打开一些 xlsx 文件时出错

iphone - 删除 UITableViewCell 和该单元格的所有数据