Database Reference
In-Depth Information
updateContextWithUbiquitousContentUpdates
is a flag that tells the stack to start or
stop listening for the notification. It has a property observer, which sets an internal
property to either the default notification center or
nil
. Using a property observer
on
that
property actually starts or stops observing the notification. This might
sound complicated, but it takes advantage of a number of Swift features to remove
a lot of checking and conditional code.
Next, since this call to
addObserver
references a selector, that function needs to
exist on the
CoreDataStack
object. Add the following straightforward method:
func
persistentStoreDidImportUbiquitousContentChanges(
notification:
NSNotification
){
NSLog
(
"Merging ubiquitous content changes"
)
context
.
performBlock
{
self
.
context
.
mergeChangesFromContextDidSaveNotification
(
notification)
}
}
With this change, enabling iCloud updates to refresh the UI context is as simple as
setting the
updateContextWithUbiquitousContentUpdates
to
true
. This will come in
handy in the
NotesListViewController
.
Open
NotesListViewController.swift
and tell the Core Data stack to start looking
for iCloud changes in
viewWillAppear
:
override
func
viewWillAppear(animated:
Bool
){
super
.
viewWillAppear
(animated)
notes
.
performFetch
(
nil
)
tableView
.
reloadData
()
stack
.
updateContextWithUbiquitousContentUpdates
=
true
}
As before, build and run CloudNotes on the device as well as in the Simulator. Try
adding a note from the Simulator, trigger an iCloud refresh, and the new note
should appear on the device within a few moments!