Database Reference
In-Depth Information
Responding to iCloud changes
The NotesListViewController is populating the table view using a
NSFetchedResultsController . This is a pattern than should be familiar to you by
now.
The fetched results controller is connected to the main NSManagedObjectContext
from the Core Data stack. When iCloud applies updates, it does so at the persistent
store level. These changes won't show up in your context unless you merge them
in.
Core Data sends out an
NSPersistentStoreDidImportUbiquitousContentChangesNotification notification
informing the app that updates have been applied to the data, and that it's time to
refresh any contexts using the modified data.
There will be a little Objective-C-style dynamic dispatch in iCloud, so you need to
make sure the Core Data stack will work with that. Open CoreDataStack.swift
and change the class declaration as follows:
@objc class CoreDataStack: Printable {
This will ensure the stack can play nicely with dynamic dispatch.
To allow the stack to observe the notifications, add the following code to
CoreDataStack.swift :
var updateContextWithUbiquitousContentUpdates: Bool = false {
willSet {
ubiquitousChangesObserver = newValue ?
NSNotificationCenter . defaultCenter () : nil
}
}
private var ubiquitousChangesObserver : NSNotificationCenter ? {
didSet {
oldValue?. removeObserver ( self , name:
NSPersistentStoreDidImportUbiquitousContentChangesNotification ,
object: coordinator )
ubiquitousChangesObserver ?. addObserver ( self ,
selector:
"persistentStoreDidImportUbiquitousContentChanges:" ,
name:
NSPersistentStoreDidImportUbiquitousContentChangesNotification ,
object: coordinator )
}
}
 
Search WWH ::




Custom Search