views:

508

answers:

1

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 affect. 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?

+1  A: 

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.

Brad Larson
Yeah I had actually thought that. But as relationship faults were being shown I thought the description would show attribute faults. Interesting, I'll have a look into that! Thanks!
Michael Waterfall
I have a quick question. For some reason it is not allowing to-many relationships, only attributes and 1-1 relationships. Is this correct or an I hitting some kind of error? I'm getting:`Invalid to many relationship...passed to setPropertiesToFetch:`
Michael Waterfall
From the documentation on -setPropertiesToFetch: "The property descriptions may represent attributes, to-one relationships, or expressions." I think for to-many relationships, they recommend using -setRelationshipKeyPathsForPrefetching:
Brad Larson
Thanks for that. Sorry, I looked at the docs for ages trying to figure it out and I can't believe I didn't see that part! Thought my docs were out of date but turns out I was just not paying enough attention! Thanks again!
Michael Waterfall