Database Reference
In-Depth Information
When Core Data sends the “will change” notification, the Core Data stack will save
any unchanged data to the current store, so the user who is logging out doesn't
lose any data. It will then reset the managed object context to clear any of the old
objects from memory.
When Core Data sends the “did change” notification, the notes list controller needs
to reload its view to either use the newly signed-in account or local storage.
You'll deal with the “will change” notification first. Add the following method to
CoreDataStack.swift that you will call when Core Data fires the notification:
func persistentStoreCoordinatorWillChangeStores(
notification: NSNotification ){
var error : NSErrorPointer = nil
if context . hasChanges {
if context . save (error) == false {
print ( "Error saving \(error) " )
}
}
context . reset ()
}
This saves the context and then resets it.
You can consider observing this notification as part of observing ubiquitous content
updates in general, so you can simply expand the property observer on
ubiquitousChangesObserver to include the new notification. Add the following code
inside the didSet closure:
oldValue?. removeObserver ( self ,
name: NSPersistentStoreCoordinatorStoresWillChangeNotification ,
object: coordinator )
ubiquitousChangesObserver ?. addObserver ( self ,
selector: "persistentStoreCoordinatorWillChangeStores" ,
name: NSPersistentStoreCoordinatorStoresWillChangeNotification ,
object: coordinator )
Now you need to deal with the “did change” notification. In
NotesListViewController.swift , add the following method, which you'll call when
you receive the notification:
func persistentStoreCoordinatorDidChangeStores(
notification: NSNotification ){
notes . performFetch ( nil )
}
Search WWH ::




Custom Search