Database Reference
In-Depth Information
Joe asks:
What Is a Fault?
Core Data faults are similar to virtual memory page faults. Faulted objects are scoped
objects that may or may not actually be in memory, or “realized,” until you actually
use them. Although there is no guarantee for when a faulted
NSManagedObject
will be
loaded into memory, it is guaranteed to be loaded when accessed. However, the object
will be an instance of the appropriate class (either an
NSManagedObject
or the designated
subclass), but its attributes are not initialized.
will be called whenever the
NSManagedObject
is “faulted,” which occurs far more
often than the object actually being removed from memory.
-finalize
-finalize
is on the list for the same reason as
-dealloc
. When dealing with
NSMan-
agedObject
objects,
-finalize
is not the proper point to be releasing resources.
Methods to Override
With the long list of methods that we should not override, what methods should
we consider overriding? There are a few methods we will commonly override.
-didTurnIntoFault
This method is called after the
NSManagedObject
has been turned into a fault.
It is a good place to release transient resources. One important thing to note
is that when this method is called, all the stored values/relationships in the
NSManagedObject
are already out of memory. If you access any of them, it will
fire the fault and pull them all back into memory again.
-willTurnIntoFault
Similar to
-didTurnIntoFault
, this method is called just before the
NSManagedObject
is turned into a fault. If your code needs to access attributes or relationships
on the
NSManagedObject
before it is turned into a fault, then this is the entry
point to use. Transient resources that impact attributes and relationships
should be released here.
-awakeFromInsert
As mentioned, overriding any of the
-init
methods is risky and unnecessary.
However, it is very useful to be able to prepare an
NSManagedObject
before it
starts accepting data. Perhaps we want to set up some logical defaults or
assign some relationships before handing the object to the user. In these sit-
uations, we use
-awakeFromInsert
. As the name implies, this method is called