ios - 为什么我的 NSFetchedResultsController 会加载所有行?
<p><p>我有一个 NSFetchedResultsController 用于在表格 View 中显示数据(22117 行)。 fetchRequest 上的 fetchBatchSize 设置为 20。它按预期工作:加载 TableView 时只有 20 行完全填充。 SQL 语句:</p>
<pre><code>2015-06-12 17:59:55.526 BoulderFinder CoreData: sql: SELECT 0, t0.Z_PK FROM ZBOULDER t0 ORDER BY t0.ZNORMALIZEDNAME
2015-06-12 17:59:55.533 BoulderFinder CoreData: annotation: sql connection fetch time: 0.0070s
2015-06-12 17:59:55.534 BoulderFinder CoreData: annotation: total fetch execution time: 0.0075s for 22117 rows.
2015-06-12 17:59:55.534 BoulderFinder
2015-06-12 17:59:55.540 BoulderFinder 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 WHEREt0.Z_PK IN(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)ORDER BY t0.ZNORMALIZEDNAME LIMIT 20
2015-06-12 17:59:55.541 BoulderFinder CoreData: annotation: sql connection fetch time: 0.0010s
2015-06-12 17:59:55.541 BoulderFinder CoreData: annotation: total fetch execution time: 0.0014s for 20 rows.
</code></pre>
<p>但是,我有另一个 View 以不同的顺序显示相同的实体(只有少数实体通过谓词过滤)。我没有在这个 View 中使用 NSFetchedResultsController。如果我加载这个 View ,其中在 managedObjectContext 中加载了大约 59 个对象,然后重新加载另一个 TableView ,NSFetchedResultsController 现在以 20 行为单位加载所有行(22117)。这里是 SQL 语句(只有第一个):</p>
<pre><code>2015-06-12 18:00:23.315 BoulderFinder CoreData: sql: SELECT 0, t0.Z_PK FROM ZBOULDER t0 ORDER BY t0.ZNORMALIZEDNAME
2015-06-12 18:00:23.322 BoulderFinder CoreData: annotation: sql connection fetch time: 0.0067s
2015-06-12 18:00:23.322 BoulderFinder CoreData: annotation: total fetch execution time: 0.0070s for 22117 rows.
2015-06-12 18:00:23.323 BoulderFinder 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 WHEREt0.Z_PK IN(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)ORDER BY t0.ZNORMALIZEDNAME LIMIT 79
2015-06-12 18:00:23.324 BoulderFinder CoreData: annotation: sql connection fetch time: 0.0009s
2015-06-12 18:00:23.324 BoulderFinder CoreData: annotation: total fetch execution time: 0.0013s for 20 rows.
2015-06-12 18:00:23.324 BoulderFinder 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 WHEREt0.Z_PK IN(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)ORDER BY t0.ZNORMALIZEDNAME LIMIT 79
2015-06-12 18:00:23.326 BoulderFinder CoreData: annotation: sql connection fetch time: 0.0012s
2015-06-12 18:00:23.326 BoulderFinder CoreData: annotation: total fetch execution time: 0.0015s for 20 rows.
...
</code></pre>
<p>我发现的唯一解决方法是在设置 NSFetchedResultsController 之前调用 resetContext。</p>
<p>相关代码下方:</p>
<pre><code>- (void)viewDidLoad
{
;
NSFetchRequest *fetchRequest = ];
NSSortDescriptor *normalizedNameSortDescriptor = ;
fetchRequest.sortDescriptors = @;
fetchRequest.fetchBatchSize = 20;
// ;
self.fetchedResultsController = [ initWithFetchRequest:fetchRequest managedObjectContext:self.boulderSearch.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
self.fetchedResultsController.delegate = nil;
// Perform fetch
NSError *error = nil;
;
if (error) {
NSLog(@"Unable to perform fetch.");
NSLog(@"%@, %@", error, error.localizedDescription);
}
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
BDBBoulder *boulder = ;
cell.textLabel.text= ;
cell.detailTextLabel.text = ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *sections = ;
id<NSFetchedResultsSectionInfo> sectionInfo = ;
return ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = ;
;
return cell;
}
</code></pre>
<p>我无法理解这种行为。</p>
<hr/>
<p>编辑:
<img src="/image/yM7Gr.png" alt="storyboard"/> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我已使用简化的应用程序再次执行测试,以确保行为。我有 2 个 VC:
- 在第一个中,我获取 fetchBatchSize 为 20 的所有对象(22117)并访问结果的第一个元素
- 在第二个中,我获取 59 个对象并修改它们的 transient 属性</p>
<p>fetchRequest.includesPendingChanges = YES</p>
<p>如果我加载第一个 VC,只有前 20 行是完全加载的:</p>
<pre><code>2015-06-16 23:13:45.520 BoulderFinder CoreData: sql: SELECT 0, t0.Z_PK FROM ZBOULDER t0
2015-06-16 23:13:45.528 BoulderFinder CoreData: annotation: sql connection fetch time: 0.0074s
2015-06-16 23:13:45.528 BoulderFinder 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 WHEREt0.Z_PK IN(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) LIMIT 20
2015-06-16 23:13:45.532 BoulderFinder CoreData: annotation: sql connection fetch time: 0.0012s
2015-06-16 23:13:45.532 BoulderFinder CoreData: annotation: total fetch execution time: 0.0021s for 20 rows.
</code></pre>
<p>现在,如果我先加载第二个 VC,然后再加载第一个 VC,那么所有行都按 20 个批处理加载。</p>
<p>但是,如果我在不修改 transient 属性的情况下进行相同的测试,那么只有前 20 行是完全加载的。</p>
<p>最后,如果 fetchRequest.includesPendingChanges = NO</p>
<p>忽略未决的更改,然后它在所有情况下都按预期工作。</p>
<p>奇怪,但这就是它的工作原理。
感谢 <a href="https://stackoverflow.com/users/3985749/pbasdf/" title="pbasdf" rel="noreferrer noopener nofollow">pbasdf</a> .</p></p>
<p style="font-size: 20px;">关于ios - 为什么我的 NSFetchedResultsController 会加载所有行?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/30809360/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/30809360/
</a>
</p>
页:
[1]