0
votes

I'm having some conceptual issues with Core Data. Up until now, I had thought that the answer to faulting on an object was that my view controller needed to have fetched that object beforehand. However, reading up on the subject it appears as though Core Data should do lazy fetching of the objects that are related to an NSManagedObject that I already have. So do I only need to fetch something at the very beginning of my application run, or is there more to it?

1
It depends very much upon what is in your database. Lazy fetching is general practice but of course there are lots of times you'd like to prefetch data if its going to take awhile. But you do only pull NSManagedObject subclasses generally. - Ryan Poolos
Yes, if it is just about loading directly related objects, regardless whether that are to-one or to-many relations, than lazy fetching works fine. It can be quite confusing while debugging when relations, that are not yet loaded, are displaied as failed or so. But as to my experience it just works. Just access the relations either using KVC or access the properties of the automatically generated subclasses without mutch thinking of the fetching. - Hermann Klecker

1 Answers

0
votes

If you just want to get object(s) from a relationship property of another object, you'd typically just access that property and Core Data will do the fetching automatically.

However, there are times when you need a set of objects that don't share the same relationship, e.g. you might want to retrieve all objects that have a date property within a certain range, or something like that. In these cases, you'd use an explicit NSFetchRequest and set its predicate.