Database Reference
In-Depth Information
Chapter 10: Multiple Managed
Object Contexts
By Matthew Morey
A managed object context is an in-memory scratchpad that you use to work with
your managed objects. In Chapter 3, “The Core Data Stack,” you learned how the
managed object context fits in with the other classes in the Core Data stack.
Most apps need but a single managed object context. A single managed object
context with a main queue, the default behavior, is simple to manage and
understand. Apps with multiple managed object contexts are harder to debug. For
that reason, you should avoid them, if possible.
That being said, certain situations do warrant the use of more than one managed
object context. For example, long-running tasks such as exporting data will block
the main thread of apps that use only a single main-queue managed object context,
causing the UI to stutter.
In other situations, such as when temporarily editing user data, it's helpful to treat
a managed object context as a set of changes that the app can just throw away if it
no longer needs them. Using child contexts makes this possible.
In this chapter, you'll learn about multiple managed object contexts by taking a
journaling app for surfers and improving it in several ways by adding multiple
contexts.
Note: If common Core Data phrases such as managed object subclass and
persistent store coordinator don't ring any bells, or if you're unsure what a
Core Data stack is supposed to do, you may want to read or reread the first
three chapters of this topic before proceeding. This chapter covers advanced
topics and assumes you already know the basics.
Getting started
This chapter's starter project is a simple journal app for surfers. After each surf
session, a surfer can use the app to create a new journal entry that records marine
 
 
Search WWH ::




Custom Search