为什么我的 NSFetchedResultsController 加载所有行?

Why my NSFetchedResultsController loads all rows?

我有一个 NSFetchedResultsController 用于在 table 视图(22117 行)中显示数据。 fetchRequest 上的 fetchBatchSize 设置为 20。它按预期工作:加载 table 视图时,只有 20 行被完全填充。 SQL 语句:

2015-06-12 17:59:55.526 BoulderFinder[2228:1220755] CoreData: sql: SELECT 0, t0.Z_PK FROM ZBOULDER t0 ORDER BY t0.ZNORMALIZEDNAME
2015-06-12 17:59:55.533 BoulderFinder[2228:1220755] CoreData: annotation: sql connection fetch time: 0.0070s
2015-06-12 17:59:55.534 BoulderFinder[2228:1220755] CoreData: annotation: total fetch execution time: 0.0075s for 22117 rows.
2015-06-12 17:59:55.534 BoulderFinder[2228:1220755]

2015-06-12 17:59:55.540 BoulderFinder[2228:1220755] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZBOULDERDESCRIPTION, t0.ZFIRSTCLIMBERFIRSTNAME, t0.ZFIRSTCLIMBERLASTNAME, t0.ZGRADE, t0.ZIMAGE, t0.ZLATITUDE, t0.ZLONGITUDE, t0.ZNAME, t0.ZNORMALIZEDAREANAME, t0.ZNORMALIZEDCIRCUITNAME, t0.ZNORMALIZEDGRADE, t0.ZNORMALIZEDNAME, t0.ZNUMBERINCIRCUIT, t0.ZAREA, t0.ZCIRCUIT FROM ZBOULDER t0 WHERE  t0.Z_PK IN  (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)  ORDER BY t0.ZNORMALIZEDNAME LIMIT 20
2015-06-12 17:59:55.541 BoulderFinder[2228:1220755] CoreData: annotation: sql connection fetch time: 0.0010s
2015-06-12 17:59:55.541 BoulderFinder[2228:1220755] CoreData: annotation: total fetch execution time: 0.0014s for 20 rows.

但是,我有另一个视图以不同的顺序显示相同的实体(只有少数实体通过谓词过滤)。我没有在此视图中使用 NSFetchedResultsController。如果我加载这个视图,在 managedObjectContext 中加载了大约 59 个对象,然后重新加载另一个 table 视图,NSFetchedResultsController 现在以 20 行为一组加载所有行 (22117)。这里有 SQL 个语句(只有第一个):

2015-06-12 18:00:23.315 BoulderFinder[2228:1220755] CoreData: sql: SELECT 0, t0.Z_PK FROM ZBOULDER t0 ORDER BY t0.ZNORMALIZEDNAME
2015-06-12 18:00:23.322 BoulderFinder[2228:1220755] CoreData: annotation: sql connection fetch time: 0.0067s
2015-06-12 18:00:23.322 BoulderFinder[2228:1220755] CoreData: annotation: total fetch execution time: 0.0070s for 22117 rows.
2015-06-12 18:00:23.323 BoulderFinder[2228:1220755] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZBOULDERDESCRIPTION, t0.ZFIRSTCLIMBERFIRSTNAME, t0.ZFIRSTCLIMBERLASTNAME, t0.ZGRADE, t0.ZIMAGE, t0.ZLATITUDE, t0.ZLONGITUDE, t0.ZNAME, t0.ZNORMALIZEDAREANAME, t0.ZNORMALIZEDCIRCUITNAME, t0.ZNORMALIZEDGRADE, t0.ZNORMALIZEDNAME, t0.ZNUMBERINCIRCUIT, t0.ZAREA, t0.ZCIRCUIT FROM ZBOULDER t0 WHERE  t0.Z_PK IN  (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)  ORDER BY t0.ZNORMALIZEDNAME LIMIT 79
2015-06-12 18:00:23.324 BoulderFinder[2228:1220755] CoreData: annotation: sql connection fetch time: 0.0009s
2015-06-12 18:00:23.324 BoulderFinder[2228:1220755] CoreData: annotation: total fetch execution time: 0.0013s for 20 rows.
2015-06-12 18:00:23.324 BoulderFinder[2228:1220755] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZBOULDERDESCRIPTION, t0.ZFIRSTCLIMBERFIRSTNAME, t0.ZFIRSTCLIMBERLASTNAME, t0.ZGRADE, t0.ZIMAGE, t0.ZLATITUDE, t0.ZLONGITUDE, t0.ZNAME, t0.ZNORMALIZEDAREANAME, t0.ZNORMALIZEDCIRCUITNAME, t0.ZNORMALIZEDGRADE, t0.ZNORMALIZEDNAME, t0.ZNUMBERINCIRCUIT, t0.ZAREA, t0.ZCIRCUIT FROM ZBOULDER t0 WHERE  t0.Z_PK IN  (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)  ORDER BY t0.ZNORMALIZEDNAME LIMIT 79
2015-06-12 18:00:23.326 BoulderFinder[2228:1220755] CoreData: annotation: sql connection fetch time: 0.0012s
2015-06-12 18:00:23.326 BoulderFinder[2228:1220755] CoreData: annotation: total fetch execution time: 0.0015s for 20 rows.
    ...

我发现的唯一解决方法是在设置 NSFetchedResultsController 之前调用 resetContext。

相关代码下方:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[BDBBoulder entityName]];
    NSSortDescriptor *normalizedNameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"normalizedName" ascending:YES];
    fetchRequest.sortDescriptors = @[normalizedNameSortDescriptor];
    fetchRequest.fetchBatchSize = 20;
    // [self.fetchedResultsController.managedObjectContext reset];
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.boulderSearch.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    self.fetchedResultsController.delegate = nil;
    // Perform fetch
    NSError *error = nil;
    [self.fetchedResultsController performFetch:&error];
    if (error) {
        NSLog(@"Unable to perform fetch.");
        NSLog(@"%@, %@", error, error.localizedDescription);
    }
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    BDBBoulder *boulder = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text  = [NSString stringWithFormat:@"%@ / %@", boulder.normalizedName,boulder.grade];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ / %@", boulder.normalizedCircuitName,boulder.normalizedAreaName];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *sections = [self.fetchedResultsController sections];
    id<NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section];

    return [sectionInfo numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"allBouldersCell" forIndexPath:indexPath];
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

我无法理解这种行为。


编辑:

我已经使用简化的应用程序再次执行测试以确保行为。我有 2 VCs: - 在第一个中,我使用 fetchBatchSize 为 20 获取所有对象 (22117) 并访问结果的第一个元素 - 在第二个中,我获取了 59 个对象并修改了它们的瞬态 属性

fetchRequest.includesPendingChanges = 是

如果我加载第一个 VC,只有前 20 行被完全加载:

2015-06-16 23:13:45.520 BoulderFinder[946:271012] CoreData: sql: SELECT 0, t0.Z_PK FROM ZBOULDER t0 
2015-06-16 23:13:45.528 BoulderFinder[946:271012] CoreData: annotation: sql connection fetch time: 0.0074s
2015-06-16 23:13:45.528 BoulderFinder[946:271012] CoreData: annotation: total fetch execution time: 0.0081s for 22117 rows.
CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZBOULDERDESCRIPTION, t0.ZFIRSTCLIMBERFIRSTNAME, t0.ZFIRSTCLIMBERLASTNAME, t0.ZGRADE, t0.ZIMAGE, t0.ZLATITUDE, t0.ZLONGITUDE, t0.ZNAME, t0.ZNORMALIZEDAREANAME, t0.ZNORMALIZEDCIRCUITNAME, t0.ZNORMALIZEDGRADE, t0.ZNORMALIZEDNAME, t0.ZNUMBERINCIRCUIT, t0.ZAREA, t0.ZCIRCUIT FROM ZBOULDER t0 WHERE  t0.Z_PK IN  (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)   LIMIT 20
2015-06-16 23:13:45.532 BoulderFinder[946:271012] CoreData: annotation: sql connection fetch time: 0.0012s
2015-06-16 23:13:45.532 BoulderFinder[946:271012] CoreData: annotation: total fetch execution time: 0.0021s for 20 rows.

现在,如果我加载第二个 VC,然后加载第一个 VC,则所有行都以 20 为一组加载。

但是,如果我在不修改瞬态 属性 的情况下进行相同的测试,那么只有前 20 行会被完全加载。

最后,如果fetchRequest.includesPendingChanges = NO

忽略未决的更改,然后它在所有情况下都按预期工作。

奇怪,但这就是它的工作原理。 感谢pbasdf.