Graphics Programs Reference
In-Depth Information
ents. (Technically, it's a little more complicated.) This dictionary is passed as the options
for addPersisten-
tStoreWithType:configuration:URL:options:error: .
We have set up Core Data to store its SQLite file in the application sandbox and its trans-
action log in a ubiquity container. Whenever a device modifies its SQLite file with Core
Data, the changes are automatically written to the transaction log in the ubiquity container
and synchronized with iCloud.
However, other devices do not automatically update their SQLite file with the changes in
the transaction log. To make this happen, BNRFeedStore needs to watch the transaction
log for changes. When a change occurs, the store will merge those changes into its own
version of the Core Data SQLite file.
Fortunately, this is pretty easy: whenever a change occurs, the notification NSPersist-
entStoreDidImportUbiquitousContentChangesNotification is posted
to the NSNotificationCenter . The store can register as an observer for this notific-
ation. In BNRFeedStore.m , modify init to register for this notification.
- (id)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(contentChange:)
name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
object:nil];
model = [NSManagedObjectModel mergedModelFromBundles:nil];
This notification will be posted whenever the transaction log is changed by another
device. If Nerdfeed isn't running on a device when the transaction log is changed, this no-
tification will be posted as soon as the device starts running Nerdfeed again.
When this notification is posted, Core Data needs merge the changes into its local SQLite
file. To merge the changes, all we have to do is pass the NSNotification object that
represents this notification directly to the Core Data context. In BNRFeedStore.m , im-
plement the message sent to BNRFeedStore when this notification occurs.
- (void)contentChange:(NSNotification *)note
{
// merge changes into context
[context mergeChangesFromContextDidSaveNotification:note];
}
Search WWH ::




Custom Search