Database Reference
In-Depth Information
right after the NSManagedObject is created from an insert call. This method is
called before any values are set and is a perfect opportunity to set default
values, initialize transient properties, and perform other tasks that we would
normally handle in the -init method. This method is called exactly once in the
entire lifetime of an object. It will not be called on the next execution of the
application, and it will not be called when an object is read in from the persis-
tent store. Therefore, we do not need to worry about overriding values that
have been set previously. When we override this method, we should be sure
to call [super awakeFromInsert] at the very beginning of our implementation to
allow the NSManagedObject to finish anything it needs to before we begin our
code.
-awakeFromFetch
-awakeFromFetch is the counterpart to -awakeFromInsert . The -awakeFromFetch method
will be called every time the object is retrieved from the persistent store (that
is, loaded from disk). This method is highly useful for setting up transient
objects or connections that the NSManagedObject will use during its life cycle.
At this point in the creation of the NSManagedObject , the observation of changes
to the object (or other objects) is turned off, and Core Data will not be aware
of any changes made. Ideally, we should avoid making any changes to rela-
tionships during this method because the inverse will not be set. However, if
we explicitly set both sides of the relationship, it is possible to make changes
here. Like the -awakeFromInsert method, when we override this method, we should
call [superawakeFromFetch]; before any of our own code is called.
1.5
NSFetchRequest
NSFetchRequest is the part of Core Data that causes people to think it is a
database API instead of an object hierarchy. When we want to retrieve objects
from Core Data, we normally use an NSFetchRequest to do the retrieval. It is best
to view an NSFetchRequest as a way to retrieve all instances of an entity from
the object hierarchy, with the option to filter the results with an NSPredicate .
There are two parts to the creation of an NSFetchRequest : setting the entity to
be retrieved and optionally defining an NSPredicate to filter the objects we want
retrieved.
Setting the Entity
One thing that we must do as part of every NSFetchRequest is define the entity
we want returned from the fetch. We do this by passing the appropriate
NSEntityDescription to the NSFetchRequest . For example, if we want retrieve Recipe
entities, we construct the NSFetchRequest as follows:
 
 
Search WWH ::




Custom Search