Michael Waterfall
Michael Waterfall

Reputation: 20569

setPropertiesToFetch method appears to always make request return faulted objects

Whenever I use setPropertiesToFetch on an NSFetchRequest, the objects that are returned appear to all be faulted. They return YES to isFaulted and when displaying them in the console they appear like this:

< MyEntity: 0x5884850> (entity: MyEntity; id: 0x5886180 <x-coredata://4D96A1CB-187C-4D92-A50C-D639F7E69114/MyEntity/p197> ; data: <fault>),

Whereas if I don't set properties to fetch, every object's properties are visible in the console. Any idea why this is happening?

Upvotes: 1

Views: 1010

Answers (3)

eofster
eofster

Reputation: 2047

The documentation says that the value from -setPropertiesToFetch: is only used when resultType is set to NSDictionaryResultType.

Upvotes: 0

Michael Waterfall
Michael Waterfall

Reputation: 20569

I've got an answer from the Apple Developer Forums:

https://devforums.apple.com/message/152330#152330

"The object being a fault means it might require a trip to the database, or it may mean that the object is pending lazy initialization and all the necessary data is cached. Tracking your app with the Core Data template in Instruments is the best way to empirically tell the difference.

For setPropertiesToFetch, you are intentionally not fetching all the properties, so you end up with a partial fault. Accessing some properties (the ones you told it to fetch) will work, and accessing others will require a round trip to the database. -isFault is a boolean, so most of the framework treats partial faults like they are just faults."

Upvotes: 2

Ken Aspeslagh
Ken Aspeslagh

Reputation: 11594

By default, Core Data will fetch objects as faults the first time they're loaded into the managed object context. It won't populate the attributes until you ask for them. If you need a fetch request to return non-faulted objects, you can use setReturnsObjectsAsFaults:YES

Upvotes: 0

Related Questions