Reputation: 2940
I have a pretty basic question. If I have two entities with a relation between them (lets say entityOne has a to-many relationship to entityTwo). When I fetch entityOne, does it automatically fetch its relationships? Or do I have to fetch them as well and assign them to its corresponding entity?
I thought this would be actually better to ask in the chat since its a simple question, but I dont have the reputation needed yet.
Since I can't post my question because its too short, I'll just ask another question.
In the same project, I have a rootViewController which fetches the entities. Now if I want to add an event I have to send the user to another view, with textFields and such. I ended up creating an array "eventArray" for each of those viewControllers since I couldn't find a more efficient way of doing this. Lets say I have in rootViewController a NSMutableArray *eventsArray and in addEventViewController I have also an NSMutableArray *eventsArray which gets set before pushing addEventViewController (addEventViewController.eventsArray = self.eventsArray). How do I do this more efficiently? I'm pretty sure this isn't the right way of doing it.
Upvotes: 3
Views: 713
Reputation: 5346
In answer to your first question:
This situation is handled by Core Data automatically, and the mechanism is called "faulting". Apple has some helpful documentation on this. Simply put, the related objects aren't fetched, but when you try to access them, Core Data will automatically retrieve them from the persistent store.
Of course, there may be situations where this one-by-one fetching of related objects (perhaps to display something about the related objects in a table for example) slows down your application.
In this situation you can use the setRelationshipKeyPathsForPrefetching:
method on NSFetchRequest
to retrieve these objects upfront. It's all a question of balancing memory usage and performance.
Faulting is a very important part of Core Data, and I'd strongly suggest reading the documentation carefully to make sure you have a good understanding of how and when it is used.
Upvotes: 3