0

I'm not new to this technology, but one thing is bugging me out. It's about Core Data. Let's assume I have db model like this:

Person <-> House <-> House details

<-> means two-way relationship and Person has many houses, each house has it's details (whatever, it's for example).

And now in Core Data when I'm reaching fro specific Person I get this Person along with all relationships (in objective approach references) down to "House details".

Now imagine that db model is more complex and XCode forces two-way relationships (only by warnings, but still).

My question is assuming if I have all relationships according to XCode right (two-way), is there any way to fetch query (from example above) in which I want to get specific Person but with out the references to house and house details.

The reason why I'm asking this question is because as far as I know it is impossible, or not? The other reason, main reason, is that when we have complex db model with two-way relationships it takes some time to grab all this data (what we want but with all the references) when we want to just receive only Person data without any other relationship(references) to db model what so ever. Basically the idea is to trim all relationships from result when we are asking only for Person data (according to example).

NOTE: This is my first post on stack overflow, so don't eat me alive :). But if it's not possible what I'm asking for, to be honest it's a big pain, not to be able to manage data as you want form the developer point of view.

If I'm not clear on this post, please let me know, I'll explain this.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
ypso
  • 5
  • 2
  • To use stack overflow well, the "preferred behaviour" is that you accept an answer that answers your question. It looks like you should do that for Warren's answer below. – Scott Corscadden Jun 01 '12 at 10:33

3 Answers3

0

You should not worry about exactly what Core Data will fetch when you get a specific instance of an entity. Core Data will manage the memory issues for you. Even the single attributes of a fetched entity might be "faults" i.e. Core Data will retrieve these details when it needs to automatically. You can check this with NSLog statements. Many details of your managed objects will not be available until needed.

So if you have an entity Person and fetch one instance person with 100 houses with 50 house details each, you will not clog your memory with all that data.

That's the beauty of Core Data, actually.

Mundi
  • 79,884
  • 17
  • 117
  • 140
0

Core data does lazy loading of relationships by default, so when you fetching your person object, your relationships will only be fetched when you start referencing them.

iTukker
  • 2,083
  • 2
  • 16
  • 16
0

The bad news is that when you have a two way relationship that relationship is maintained from both ends so in your case you can add a person to a house or a house to a person and the either side will have it in their relationship set ( assuming many to many relationships ). It's the way that Core Data works.

The good news is it really doesn't matter due to way CoreData fetches information lazily. So in the case where you wish to get a person without fetching all it's related houses that's what CoreData does. Until the point that you actually wish to load the objects at the end of a relationship they exist as Faults which is to say minimal skeleton references.

NSLog one of your fetched Person objects and if it's related houses haven't been previously fetched they will be printed out as minimal references.

Warren Burton
  • 17,451
  • 3
  • 53
  • 73
  • since now I have 3 answers with none hate. It's good. I'm answering to you because you gaved me most sophisticated answer that I wanted to hear. So my expepantation about memory consumtion are wrong, it;s good. And I'm thankful foe all the responses. But in my opinion it would be nice to have more control over the data from a person that likes opering on dbms systems insted of db over ORM over Core Data. Thanks for answers, it hepl me a lot, because I always was wondering how the memory is managed in Core Data, and I received an answer. I wrote that I'm not new, but there is so much to lear. – ypso May 31 '12 at 22:31