Database Reference
In-Depth Information
2. Next, you call
performBlock
. This function asynchronously performs the given
block on the context's queue. In this case, the queue is private.
3. Just as before, you retrieve all
JournalEntry
entities by executing a fetch
request. But this time, you use the private context to execute the fetch request.
Next, find the following code in the same function:
self
.
navigationItem
.
leftBarButtonItem
=
self
.
exportBarButtonItem
()
println
(
"Export Path:
\(exportFilePath)
"
)
self
.
showExportFinishedAlertView
(exportFilePath)
Now replace it with the following:
// 4
dispatch_async
(
dispatch_get_main_queue
(), { () -> Void
in
self
.
navigationItem
.
leftBarButtonItem
=
self
.
exportBarButtonItem
()
println
(
"Export Path:
\(exportFilePath)
"
)
self
.
showExportFinishedAlertView
(exportFilePath)
})
}
// 5 closing brace for performBlock()
4. You should always perform all operations related to the UI, such as showing an
alert view when the export operation is finished, on the main queue; otherwise
unpredictable things will happen. You use the
dispatch_async
and
dispatch_get_main_queue
to show the final alert view message on the main
queue.
5. Finally, the block you opened earlier in step 2 via the
performBlock
call now
needs to be closed with a closing curly brace.
Note:
There are three concurrency types a managed object context can use:
ConfinementConcurrencyType
specifies that the context will use the thread
confinement pattern and that the developer will be responsible for managing
all thread access. You should consider this type deprecated and never use it,
as the next two types will cover all use cases.
PrivateQueueConcurrencyType
specifies that the context will be associated with
a private dispatch queue instead of the main queue. This is the type of queue
you just used to move the export operation off of the main queue so that it no
longer interferes with the UI.
MainQueueConcurrencyType
, the default type, specifies that the context will be
associated with the main queue. This type is what the main context