我有一个提取请求,该请求从Person实体中提取一堆对象,然后遍历每个对象以查找相关对象中的某些更改。所有这些都在后台队列中发生,其类型为NSPrivateQueueConcurrencyType
的托管对象上下文。
为了对此进行优化,我想使用setRelationshipKeyPathsForPrefetching
,以便该应用程序不会每次都一直访问数据库,但似乎无法正常工作!当查看SQL调试日志时,无论是否设置了setRelationshipKeyPathsForPrefetching
,我都会看到相同的输出:
CoreData: annotation: to-many relationship fault "addresses" for objectID 0x9d51b4e7ba39580c <x-coredata://C2C3C38B-31CB-4B1B-8222-63E8B04BB9E8/Person/p1> fulfilled from database. Got 3 rows
....
CoreData: annotation: fault fulfilled from database for : 0x9d51b4e7ba395808 <x-coredata://C2C3C38B-31CB-4B1B-8222-63E8B04BB9E8/PersonAddress/p1>
这是我设置NSFetchRequest的方法:
__block NSArray *items = nil;
[context performBlockAndWait: ^{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext: context];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat: @"isContactArchived == FALSE"]];
[fetchRequest setRelationshipKeyPathsForPrefetching:@[@"emails", @"phones", @"addresses"]]; // doesn't seem to make a difference (possible because no prefetching is happening according to sql logs?)
NSError *error = nil;
NSArray items = [context executeFetchRequest:fetchRequest error:&error];
NSLog(@"Fetched = %li objects", (unsigned long)items.count);
}];
有任何想法为什么setRelationshipKeyPathsForPrefetching
不起作用?我知道我以前有过它,但是那是因为它是一个与NSFetchedResultsController绑定的NSFetchRequest,它可以在主线程上工作,因此将MOC与'NSMainQueueConcurrencyType'一起使用。