Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setPropertiesToFetch doesn't seem to have any effect

I'm trying to use setPropertiesToFetch in my fetch request to limit the data that is retrieved from my store, but it seems to have no effect. When I use it and display the object returned into the console, I can see all my properties are there. The same data is displayed whether I set the properties or not. All the relationships display as faults, but all the data for the attributes is there.

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:context];
NSDictionary *entityProperties = [entity propertiesByName];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
[fetchRequest setIncludesPendingChanges:NO];
[fetchRequest setReturnsObjectsAsFaults:NO];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:[entityProperties objectForKey:@"myAttrib"], nil]];

The fetch seems to return the same data per object with or without that last line. Any ideas?

like image 758
Michael Waterfall Avatar asked Dec 07 '09 16:12

Michael Waterfall


2 Answers

The Special Considerations section of the documentation for setPropertiesToFetch: says

This value is only used if resultType is set to NSDictionaryResultType.

Your code snippet isn't setting the resultType. Perhaps you meant to use setRelationshipKeyPathsForPrefetching:?

like image 87
Jeffrey Harris Avatar answered Nov 03 '22 18:11

Jeffrey Harris


The impression I had (from what the Apple engineers have said) was that the data would be faulted in for the non-fetched properties as soon as you used the accessors for that property. It may be that in generating the description of the NSManagedObject, these accessors are being used for each property, causing the data to be faulted in right before the string describing the objects is generated.

You could try using the Core Data Faults and / or Core Data Cache Misses instruments (in the simulator) to see when the faults actually occur. If they happen right before you print out the managed objects, that would seem to support my guess above.

like image 2
Brad Larson Avatar answered Nov 03 '22 17:11

Brad Larson