`RelationsKeyPathsForPrefetching` не работает - PullRequest
2 голосов
/ 10 июня 2019

Я хочу отобразить список сущностей из Базовых данных в 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

Я проверил документацию ссылка и нашел метод "предварительной выборки". Звучит отлично.

Я использовал это в коде:

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

PS

Меняя returnsObjectsAsFaults на true\false, не исправляйте это.

PS2

Я использую iOS10 CoreData API NSPersistentContainer. Я получаю viewContext.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...