Database Reference
In-Depth Information
changed. For example, if we store the first and last names of a user in our
Core Data repository, it might make sense to store the full name as well.
Intelligent Relationships
Relationships in a Core Data model are like salt in a cooking recipe. Too much
and you ruin the recipe; too little and something is missing. Fortunately,
there are some simple rules we can follow when it comes to relationships in
a Core Data repository.
Follow the Object Model
Core Data is first and foremost an object model. The entities in our model
should represent the data as accurately as possible. Just because a value
might be duplicated across several objects (or rows from the database point
of view) does not mean it should be extruded into its own table. Many times
it is more efficient for us to store that string several times over in the entity
itself than to traverse a relationship to get it.
Traversing a relationship is generally more expensive than accessing an
attribute on the entity. Therefore, if the value being stored is simple, it's better
to leave it in the entity it is associated with.
Separate Commonly Used from Rarely Used Data
If the object design calls for a one-to-many relationship or a many-to-many
relationship, we should definitely create a relationship for it. This is usually
the case where the data is more than a single property or contains binary
data or would be difficult to properly model inside the parent object. For
example, if we have a user entity, it is more efficient to store the user's address
in its own object as opposed to having several attributes in the user object
for address, city, state, postal code, and so on.
A balance needs to be carefully maintained between what is stored on the
other end of a relationship and what is stored in the primary entity. Crossing
key paths is more expensive than accessing attributes, but creating objects
that are very wide also slows down data access.
4.3
Fetching
Fetching is the term used to describe the resolving of NSManagedObject objects
from the repository. When we retrieve an NSManagedObject , it is “fetched” into
memory, and we can then access its properties. To help us utilize memory
efficiently, fetching may not always happen all at once. Specifically, when we
are using a SQLite store, it is quite possible that an object we think is in
 
 
Search WWH ::




Custom Search