Database Reference
In-Depth Information
-performBlock:
The purpose of -performBlock: is to allow code to execute on the correct thread
for the associated NSManagedObjectContext . By utilizing this method, we can
properly and safely access an NSManagedObjectContext without necessarily being
on its thread. The -performBlock: executes the block on the thread associated
with the NSManagedObjectContext . Note that this method does not block the calling
thread. It should also be noted that this method is not “reentrant.” For
example, if you call -performBlock: within a -performBlock: , the new block is added
to the end of the queue, as opposed to executing immediately.
-performBlockAndWait:
Like its counterpart -performBlock: , -performBlockAndWait: allows us to execute code
against an NSManagedObjectContext regardless of the thread we are currently on.
The primary difference between -performBlockAndWait: and -performBlock: is that
-performBlockAndWait: is a blocking call. The calling thread waits for the block to
finish execution before it continues. This also makes -performBlockAndWait:
reentrant. You can nest -performBlockAndWait: calls infinitely, and they will execute
in the order they are called.
Saving the NSManagedObjectContext
With the introduction of parent and child NSManagedObjectContext instances, we
need to explore how saving works. While we continue to use -save: to commit
changes in an NSManagedObjectContext , the result can vary depending on the
context.
To start with, if we save an NSManagedObjectContext that is associated with an
NSPersistentStoreCoordinator , the changes are written to the NSPersistentStoreCoordinator ,
which generally means the changes are written to disk.
However, when we call -save: on a child context that is not associated with an
NSPersistentStoreCoordinator , the changes are not written to the NSPersistentStoreCoor-
dinator . Instead, those changes are “pushed up” one level to the parent of the
current NSManagedObjectContext . When the changes are pushed up, they effectively
dirty the parent NSManagedObjectContext , and its -hasChanges method will then
return YES . It should be noted that while the changes will get pushed up to
the parent context, they will not get pushed down to any existing children. It
is best to treat existing children as “snapshots” of the data taken at the time
that the child was created.
 
 
Search WWH ::




Custom Search