用于更改NSFetchedResultsController的获取请求和重新加载表数据的配方

时间:2012-04-16 08:39:43

标签: ios uitableview nsfetchedresultscontroller nsfetchrequest reloaddata

来自apple doc Modifying the Fetch Request我发现可以更改NSFetchRequest的{​​{1}}。步骤很容易设置。

调用NSFetchedResultsController后,我认为需要在表格视图上调用performFetch:。如何进行这样的通话?

阅读一些stackoverflow主题,我已经看到在大多数情况下调用该方法应该有效。但有没有正确的方法呢?

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

  

请确保在您之前将tableview本身发送为beginUpdates   完成后交换控制器,然后是endUpdates。这个   防止表在窄窗口中询问数据时   FRC正在被换掉。然后调用reloadData。

你能准确解释一下这是什么意思吗?

1 个答案:

答案 0 :(得分:8)

假设生成正确获取(某种条件语句)的逻辑位于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];
}