ios – 核心数据子上下文不预取关系

ios – 核心数据子上下文不预取关系,第1张

概述我在我的应用程序方案中激活了Core Data调试器-com.apple.CoreData.SQLDebug 1,并获得了一个名为Category的实体的以下结果,其中包含一个名为 Image的关系实体: 主要上下文中的FetchRequest: NSFetchRequest *fr = [[NSFetchRequest alloc] initWithEntityName:@"Category"] 我在我的应用程序方案中激活了Core Data调试器-com.apple.CoreData.sqlDeBUG 1,并获得了一个名为category的实体的以下结果,其中包含一个名为 Image的关系实体:

主要上下文中的FetchRequest:

NSFetchRequest *fr = [[NSFetchRequest alloc] initWithEntityname:@"category"];fr.relationshipkeypathsForPrefetching = @[@"image"];NSArray *results = [self.mainContext executeFetchRequest:fr error:nil];for (category *category in results) {    NSLog(@"%@",category.image.wIDth);}

控制台日志显示未完成任何故障 – 自映像关系设置为预取以来的预期行为.

对子上下文的相同请求:

NSManagedobjectContext *privateMOC = [[NSManagedobjectContext alloc] initWithConcurrencyType:nsprivateQueueConcurrencyType];[privateMOC setParentContext:self.mainContext];[privateMOC performBlock:^{        NSFetchRequest *fr = [[NSFetchRequest alloc] initWithEntityname:@"category"];        fr.relationshipkeypathsForPrefetching = @[@"image"];        NSArray *results = [privateMOC executeFetchRequest:fr error:nil];        for (category *category in results) {            NSLog(@"%@",category.image.wIDth);        }    }];

在这种情况下,控制台显示Core Data为每个图像(其中4个)执行故障.这是一个错误,预期的行为还是我错过了什么?

解决方法 实验:

我已经在测试应用程序中复制了您的场景并得出了相同的结论:预取不会在后台线程中发生.日志:

******************************** FOREGROUND **************************************CoreData: sql: SELECT 0,t0.Z_PK,t0.Z_OPT,t0.ZTIMESTAMP,t0.ZTitle,t0.ZIMAGE FROM Zcategory t0 CoreData: annotation: sql connection fetch time: 0.0004sCoreData: annotation: Bound intarray values.CoreData: sql: SELECT 0,t0.Zname,t0.ZURL FROM ZIMAGE t0 WHERE  t0.Z_PK IN (SELECT * FROM _Z_intarray0)  CoreData: annotation: sql connection fetch time: 0.0006sCoreData: annotation: total fetch execution time: 0.0010s for 4 rows.CoreData: annotation: Prefetching with key 'image'.  Got 4 rows.CoreData: annotation: total fetch execution time: 0.0035s for 4 rows.******************************** BACKGROUND **************************************CoreData: sql: SELECT 0,t0.ZIMAGE FROM Zcategory t0 CoreData: annotation: sql connection fetch time: 0.0003sCoreData: annotation: total fetch execution time: 0.0005s for 4 rows.

分析:

根据NSFetchRequest的documentation,解释了提供预取以解决特定的性能挑战.它描述如下(我的重点):

Prefetching allows Core Data to obtain related objects in a single fetch (per entity),rather than incurring subsequent access to the store for each indivIDual record as their faults are tripped. For example,given an Employee entity with a relationship to a Department entity,if you fetch all the employees then for each print out their name and the name of the department to which they belong,it may be that a fault has to be fired for each indivIDual Department object (for more details,see Core Data Performance in Core Data Programming GuIDe). This can represent a significant overhead. You Could avoID this by prefetching the department relationship in the Employee fetch…

从其措辞可以看出,这不是提高性能的必要设备.这可以解释为什么它没有在后台线程上实现:讨论中描述的性能问题似乎表明涉及UI更新的典型场景.在我看来,这澄清了这个功能的意图.

如果您已经在后台线程中,那么这种性能调优肯定不那么重要,并且通常的故障机制可以充分地处理必要的优化.因此属性relationshipkeypathsForPrefetching将恢复为其默认值,即空数组.

总结

以上是内存溢出为你收集整理的ios – 核心数据子上下文不预取关系全部内容,希望文章能够帮你解决ios – 核心数据子上下文不预取关系所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1103630.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存