Database Reference
In-Depth Information
Just like with the
UIDocumentStateChangedNotification
for the
UIManagedDocument
, it's
a good idea to start listening for the
NSPersistentStoreDidImportUbiquitousContentChanges-
Notification
notifications after the Core Data stack (or
UIManagedDocument
) has
been constructed. Therefore, we put the
-addObserver:selector:name:object:
call in
the
-contextInitialized
method.
When the notification fires, it can be treated exactly as if a notification from
an
NSManagedObjectContext
is coming in from another thread, as discussed in
Chapter 5,
Threading
, on page 77
. Although the notification does not contain
actual
NSManagedObject
instances, it does contain
NSManagedObjectID
instances,
and the
NSManagedObjectContext
knows how to consume them as well.
NSManagedObjectContext *moc = [self managedObjectContext];
[moc performBlock:^{
[moc mergeChangesFromContextDidSaveNotification:notification];
}];
}
6.4
Under the Hood
Now that we have looked at how to integrate iCloud into our Core Data-based
application, it is helpful to understand exactly how it works under the hood.
Debug Output
Core Data has several logging levels that we can turn on to watch all of the
SQL calls that are generated during an application's life cycle. There are
currently three levels to this debugging log, with level 1 being the least chatty
and level 3 being the most chatty. We can set this debug output by adding
the runtime parameter
com.apple.CoreData.SQLDebug
to our application and pass
along with it the level we want to be set.
In addition to the Core Data debug setting, we can also turn on an additional
setting to watch all of the chatter
between
Core Data and iCloud. That addi-
tional setting is
com.apple.coredata.ubiquity.logLevel
. For an unknown reason, this
logging code responds only to level 3, as shown in
Figure 18,
Turning on iCloud
debugging
, on page 111
.
Looking at these logs, we can see there is a tremendous amount of activity
going on behind the scenes. We can also examine the frequency with which
Core Data starts a sync with iCloud and use that information when we are
determining how often to generate saves from our application.