Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetched Properties v Relationships (Core Data - iPhone)

I'm a new iPhone developer (of about 4 months or so) who is starting to look at Core Data.

In the "Beginning iPhone 3 Development" book by Dave Mark it mentions that the main difference between fetched properties and relationships is that fetched properties allow lazy loading. However, I have seen other resources and accepted answers on this site which suggest that relationships do allow lazy loading.

For example, if I have a Department object with a to-many relationship with Employee then I would not want a fetch on a given Department to load all the associated employees into memory.

I would be extremely grateful for an authoritative answer on this point, and on the other differences, as it has obvious implications on any design.

Also, I would be extremely grateful if someone could point me towards a reasonable Core Data resource that is a clear and beginner friendly guide (I have been a Java Developer previously but I am new to the iPhone-Way...

like image 556
Urizen Avatar asked Nov 15 '09 12:11

Urizen


People also ask

What is fetched property in Core Data?

Fetched Properties in Core Data are properties that return an array value from a predicate. A fetched property predicate is a Core Data query that evaluates to an array of results.

What are relationships in Core Data?

Inverse relationships enable Core Data to propagate change in both directions when an instance of either the source or destination type changes. Every relationship must have an inverse. When creating relationships in the Graph editor, you add inverse relationships between entities in a single step.

What is transient in Core Data?

Transient attributes are properties that you define as part of the model, but that are not saved to the persistent store as part of an entity instance's data. Core Data does track changes you make to transient properties, so they are recorded for undo operations.


2 Answers

From the "Core Data Programming Guide", fetched properties represent "weak, one-way relationships". They simply allow you to define the value of a property with a fetch predicate rather than modeling it directly like a relationship.

One major difference is that fetched properties are not live or dynamic like direct relationships. You must explicitly keep fetched properties up-to-date by manually refreshing the object containing the fetched property when changes occur in your object graph that will affect the value of the fetched property.

like image 84
Richard Stahl Avatar answered Oct 20 '22 01:10

Richard Stahl


Check out faulting in Core Data:

Using faults, if you retrieve a single Employee object from a persistent store its manager, department, and reports relationships are initially represented by faults. Although the fault is an instance of the Department class, it has not yet been realized—none of its persistent instance variables have yet been set.

If you send the Department object a message to get, say, its name, then the fault fires—and in this situation Core Data executes a fetch for you to retrieve all the object's attributes.

like image 41
Alex Reynolds Avatar answered Oct 20 '22 00:10

Alex Reynolds