我想在UITableView中显示核心数据中的实体列表。在表格的每个单元格中,我要显示来自实体的信息,但也要显示来自实体的关系实体的其他信息。
这是一个问题,因为在滚动UITableView CoreDate时,一一询问实体的关系。
CoreData: annotation: fault fulfilled from database for : 0x925e06e26f1f7feb <x-coredata://D99D4673-2788-4E00-90D6-811007DDF5AA/AnimalActivitySettingBase/p23121>
CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZACTIVITYTAGID, t0.ZACTIVITYTAGSTATUS t0 WHERE t0.Z_PK = ?
CoreData: annotation: sql connection fetch time: 0.0002s
CoreData: annotation: total fetch execution time: 0.0003s for 1 rows.
CoreData: annotation: fault fulfilled from database for : 0x925e06e240bb7feb <x-coredata://D99D4673-2788-4E00-90D6-811007DDF5AA/AnimalActivitySettingBase/p20920>
CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZACTIVITYTAGID, t0.ZACTIVITYTAGSTATUS t0 WHERE t0.Z_PK = ?
CoreData: annotation: sql connection fetch time: 0.0002s
CoreData: annotation: total fetch execution time: 0.0003s for 1 rows.
CoreData: annotation: fault fulfilled from database for : 0x925e06e287177feb <x-coredata://D99D4673-2788-4E00-90D6-811007DDF5AA/AnimalActivitySettingBase/p24659>
CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZACTIVITYTAGID, t0.ZACTIVITYTAGSTATUS t0 WHERE t0.Z_PK = ?
CoreData: annotation: sql connection fetch time: 0.0002s
CoreData: annotation: total fetch execution time: 0.0003s for 1 rows.
CoreData: annotation: fault fulfilled from database for : 0x925e06e2ce6b7feb <x-coredata://D99D4673-2788-4E00-90D6-811007DDF5AA/AnimalActivitySettingBase/p29196>
CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZACTIVITYTAGID, t0.ZACTIVITYTAGSTATUS t0 WHERE t0.Z_PK = ?
CoreData: annotation: sql connection fetch time: 0.0002s
CoreData: annotation: total fetch execution time: 0.0004s for 1 rows.
CoreData: annotation: fault fulfilled from database for : 0x925e06e281bf7feb <x-coredata://D99D4673-2788-4E00-90D6-811007DDF5AA/AnimalActivitySettingBase/p25081>
CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZACTIVITYTAGID, t0.ZACTIVITYTAGSTATUS t0 WHERE t0.Z_PK = ?
CoreData: annotation: sql connection fetch time: 0.0003s
我检查了文档link,发现了“预取”技术。听起来不错。
我在代码中使用了它:
let fetchRequest = Animal.fetchRequest()
fetchRequest.predicate = predicate
fetchRequest.sortDescriptors = sortDesc
fetchRequest.relationshipKeyPathsForPrefetching = ["animalActivitySettingBase"]
然后我再次检查了日志:
CoreData: annotation: sql connection fetch time: 0.1825s
CoreData: annotation: total fetch execution time: 0.1862s for 10437 rows.
CoreData: annotation: Prefetching with key 'animalActivitySettingBase'. Got 10437 rows.
CoreData: annotation: total fetch execution time: 0.5184s for 11031 rows.
那太好了。关系已被预取。但是...
当我向下滚动UITableView时,CoreData再次询问:
CoreData: annotation: fault fulfilled from database for : 0x925e06e26f1f7feb <x-coredata://D99D4673-2788-4E00-90D6-811007DDF5AA/AnimalActivitySettingBase/p23121>
CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZACTIVITYTAGID, t0.ZACTIVITYTAGSTATUS t0 WHERE t0.Z_PK = ?
CoreData: annotation: sql connection fetch time: 0.0002s
CoreData: annotation: total fetch execution time: 0.0003s for 1 rows.
CoreData: annotation: fault fulfilled from database for : 0x925e06e240bb7feb <x-coredata://D99D4673-2788-4E00-90D6-811007DDF5AA/AnimalActivitySettingBase/p20920>
CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZACTIVITYTAGID, t0.ZACTIVITYTAGSTATUS t0 WHERE t0.Z_PK = ?
CoreData: annotation: sql connection fetch time: 0.0002s
CoreData: annotation: total fetch execution time: 0.0003s for 1 rows.
CoreData: annotation: fault fulfilled from database for : 0x925e06e287177feb <x-coredata://D99D4673-2788-4E00-90D6-811007DDF5AA/AnimalActivitySettingBase/p24659>
CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZACTIVITYTAGID, t0.ZACTIVITYTAGSTATUS t0 WHERE t0.Z_PK = ?
CoreData: annotation: sql connection fetch time: 0.0002s
CoreData: annotation: total fetch execution time: 0.0003s for 1 rows.
CoreData: annotation: fault fulfilled from database for : 0x925e06e2ce6b7feb <x-coredata://D99D4673-2788-4E00-90D6-811007DDF5AA/AnimalActivitySettingBase/p29196>
CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZACTIVITYTAGID, t0.ZACTIVITYTAGSTATUS t0 WHERE t0.Z_PK = ?
CoreData: annotation: sql connection fetch time: 0.0002s
CoreData: annotation: total fetch execution time: 0.0004s for 1 rows.
CoreData: annotation: fault fulfilled from database for : 0x925e06e281bf7feb <x-coredata://D99D4673-2788-4E00-90D6-811007DDF5AA/AnimalActivitySettingBase/p25081>
CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZACTIVITYTAGID, t0.ZACTIVITYTAGSTATUS t0 WHERE t0.Z_PK = ?
CoreData: annotation: sql connection fetch time: 0.0003s
将returnsObjectsAsFaults
更改为true\false
并不能解决问题。
我正在使用iOS10 CoreData API NSPersistentContainer
。我正在获取viewContext
。