I have a fetch request that fetches a bunch of objects from the Person entity, and then goes through each object looking for certain changes in related objects. This all happens in a background queue, with a managed object context of type NSPrivateQueueConcurrencyType. To optimize this, I want to use setRelationshipKeyPathsForPrefetching so the app doesn't keep hitting the database each time, but it seems to not work! When I look at the SQL debug logs, I see the same output, no matter if I have setRelationshipKeyPathsForPrefetching set or not:
This is how I set the NSFetchRequest:
Any ideas why setRelationshipKeyPathsForPrefetching wouldn't work? I know I've had it working before, but that was because it was a NSFetchRequest tied to an NSFetchedResultsController that worked off the main thread, and hence used a MOC with 'NSMainQueueConcurrencyType'. This fetch request is made on a background queue.
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>
This is how I set the NSFetchRequest:
Code Block __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); }];
Any ideas why setRelationshipKeyPathsForPrefetching wouldn't work? I know I've had it working before, but that was because it was a NSFetchRequest tied to an NSFetchedResultsController that worked off the main thread, and hence used a MOC with 'NSMainQueueConcurrencyType'. This fetch request is made on a background queue.