我有一个 NSFetchedResultsController 用于在表格 View 中显示数据(22117 行)。 fetchRequest 上的 fetchBatchSize 设置为 20。它按预期工作:加载 TableView 时只有 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.
但是,我有另一个 View 以不同的顺序显示相同的实体(只有少数实体通过谓词过滤)。我没有在这个 View 中使用 NSFetchedResultsController。如果我加载这个 View ,其中在 managedObjectContext 中加载了大约 59 个对象,然后重新加载另一个 TableView ,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)configureCellUITableViewCell *)cell atIndexPathNSIndexPath *)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)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
NSArray *sections = [self.fetchedResultsController sections];
id<NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier"allBouldersCell" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
我无法理解这种行为。
编辑:
Best Answer-推荐答案 strong>
我已使用简化的应用程序再次执行测试,以确保行为。我有 2 个 VC:
- 在第一个中,我获取 fetchBatchSize 为 20 的所有对象(22117)并访问结果的第一个元素
- 在第二个中,我获取 59 个对象并修改它们的 transient 属性
fetchRequest.includesPendingChanges = YES
如果我加载第一个 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 个批处理加载。
但是,如果我在不修改 transient 属性的情况下进行相同的测试,那么只有前 20 行是完全加载的。
最后,如果 fetchRequest.includesPendingChanges = NO
忽略未决的更改,然后它在所有情况下都按预期工作。
奇怪,但这就是它的工作原理。
感谢 pbasdf .
关于ios - 为什么我的 NSFetchedResultsController 会加载所有行?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/30809360/
|