gpt4 book ai didi

iphone - 核心数据获取不一致

转载 作者:行者123 更新时间:2023-11-29 04:35:06 26 4
gpt4 key购买 nike

我正在处理一个非常小的示例项目,可以是 downloaded from here .我想做的是调试为什么在我自己的项目中获取花费了这么长时间,但我已经使用这个项目作为一种基准。我在我的方案中启用了 -com.apple.CoreData.SQLDebug 1,这样我就可以看到哪些命令通过我的提取发送到 SQLite 存储。

首先,在 AppDelegate 中,我将第一部分(正在创建银行的位置)包装在 for 循环中以创建 1000 个对象。他们注释掉来自 AppDelegate 的获取请求。

然后,在 FBCFMasterViewController 中,有一个获取请求。保存 1000 个项目并将它们提取到 NSFetchedResultsController 后运行应用程序时,您将看到数千个类似于以下的输出行:

2012-06-23 09:16:30.374 FailedBankCD[90166:fb03] CoreData: annotation: sql connection fetch time: 0.0004s
2012-06-23 09:16:30.375 FailedBankCD[90166:fb03] CoreData: annotation: total fetch execution time: 0.0009s for 1 rows.
2012-06-23 09:16:30.375 FailedBankCD[90166:fb03] CoreData: annotation: fault fulfilled from database for : 0xc3e9060 <x-coredata://60E5B64A-83AB-4E6A-BEAD-5FFF50DB7F66/FailedBankDetails/p823>
2012-06-23 09:16:30.376 FailedBankCD[90166:fb03] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZCLOSEDATE, t0.ZUPDATEDATE, t0.ZZIP, t0.ZINFO FROM ZFAILEDBANKDETAILS t0 WHERE t0.Z_PK = ?

我猜测这是因为 sortDescriptor 是用关键细节初始化的。closeDate,这是一种关系,因此当它按该属性排序时,它必须获取每个对象的错误,从而导致数千行输出出现。

因此,我将 sortDescriptor 更改为对属性 city 进行排序,这次运行时,我得到以下输出:

2012-06-23 08:53:04.924 FailedBankCD[89564:fb03] CoreData: sql: SELECT 0, t0.Z_PK FROM ZFAILEDBANKINFO t0 ORDER BY t0.ZCITY DESC
2012-06-23 08:53:04.929 FailedBankCD[89564:fb03] CoreData: annotation: sql connection fetch time: 0.0049s
2012-06-23 08:53:04.930 FailedBankCD[89564:fb03] CoreData: annotation: total fetch execution time: 0.0059s for 1009 rows.
2012-06-23 08:53:04.936 FailedBankCD[89564:fb03] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZCITY, t0.ZNAME, t0.ZSTATE, t0.ZDETAILS FROM ZFAILEDBANKINFO t0 WHERE t0.Z_PK IN (?,?,?,?,?,?,?,?,?,?) ORDER BY t0.ZCITY DESC LIMIT 10
2012-06-23 08:53:04.938 FailedBankCD[89564:fb03] CoreData: annotation: sql connection fetch time: 0.0013s
2012-06-23 08:53:04.939 FailedBankCD[89564:fb03] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZCLOSEDATE, t0.ZUPDATEDATE, t0.ZZIP, t0.ZINFO FROM ZFAILEDBANKDETAILS t0 WHERE t0.ZINFO IN (?,?,?,?,?,?,?,?,?,?)
2012-06-23 08:53:04.971 FailedBankCD[89564:fb03] CoreData: annotation: sql connection fetch time: 0.0318s
2012-06-23 08:53:04.972 FailedBankCD[89564:fb03] CoreData: annotation: total fetch execution time: 0.0326s for 10 rows.
2012-06-23 08:53:04.973 FailedBankCD[89564:fb03] CoreData: annotation: Prefetching with key 'details'. Got 10 rows.
2012-06-23 08:53:04.974 FailedBankCD[89564:fb03] CoreData: annotation: total fetch execution time: 0.0372s for 10 rows.

我不再重复数千行输出,这似乎是预期的输出。但是,如果我再次运行,甚至没有修改任何内容,我会再次获得数千行类似于以下内容的输出:

2012-06-23 09:19:31.263 FailedBankCD[90216:fb03] CoreData: annotation: sql connection fetch time: 0.0006s
2012-06-23 09:19:31.264 FailedBankCD[90216:fb03] CoreData: annotation: total fetch execution time: 0.0010s for 20 rows.
2012-06-23 09:19:31.264 FailedBankCD[90216:fb03] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZCITY, t0.ZNAME, t0.ZSTATE, t0.ZDETAILS FROM ZFAILEDBANKINFO t0 WHERE t0.Z_PK IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ORDER BY t0.ZCITY DESC LIMIT 20

这是奇怪的行为,不是吗?任何人都可以快速复制此内容并解释发生了什么以及为什么我会得到数千行输出?

最佳答案

close_datecity 未建立索引,这解释了数千个查询,并且可能导致性能低下。对于运行之间的差异,我敢打赌您正在使用的缓存:

NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext sectionNameKeyPath:nil
cacheName:@"Root"];

关于iphone - 核心数据获取不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11170236/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com