Search code examples
iosobjective-cswiftcore-data

Is it possible to fetch an object without its relationships in Core Data?


Let's say I have one entity "Person" with many attributes and one relationship "shoes" to another entity "Shoe".

The "Person" entity has thousands of "Shoe"s. I have sometimes the need to retrieve just the attributes of this person but I don't need to download all the shoes.

Is it possible to fetch only a "Person" without its relationship "Shoe"?

And consequently, is it possible to fetch the relationship "Shoe" once we already fetched the "Person"?


Solution

  • What you are describing is how Core Data works by default. If you use a fetch request to get a Person, then the returned object's shoes property is a “fault”. A fault is an empty shell. When you first try to access a fault's contents (its properties or, in the case of a collection, its member objects), the fault “fires” by loading its contents from the database.

    Read about faults in the Core Data Programming Guide.