Database Reference
In-Depth Information
// 2
let
childEntry = childContext.
objectWithID
(
surfJournalEntry.
objectID
)
as
JournalEntry
// 3
detailViewController.
journalEntry
= childEntry
detailViewController.
context
= childContext
detailViewController.
delegate
=
self
Let's go through the modified segue code step by step:
1. First, you create a new managed object context called
childContext
with a
MainQueueConcurrencyType
. Instead of setting a persistent store coordinator as
you normally do when creating a managed object context, you set a parent
context. Here, you set the
parentContext
to the main context.
2. Using the child context's
objectWithID
function, you retrieve the relevant journal
entry. You must use
objectWithID
to retrieve the journal entry because managed
objects are specific to the context that created them. However,
objectID
values
are not specific to a single context, so you can use them when you need to
access objects in multiple contexts.
3. Finally, you set all needed variables on the
JournalEntryViewController
instance.
This time, you use the
childEntry
and
childContext
instead of the original
surfJournalEntry
and
surfJournalEntry.managedObjectContext
.
Note:
You might be wondering why you need to pass both the managed
object and the managed object context to the
detailViewController
since
managed objects already have a context variable. You need to pass the
context because managed objects only have a weak reference to the context.
If you don't pass the context, ARC will remove the context from memory
(since nothing else is retaining it) and the app will not behave as expected.
It's time to build and run! Give it a go.
Just like before, the app should behave exactly the same. In this case, no visible
changes to the app are a good thing. The user can still tap on a row to view and
edit a surf session journal entry.