Skip to content

Working with Managed Object Contexts

Tony Arnold edited this page May 17, 2014 · 5 revisions

Creating New Contexts

A variety of simple class methods are provided to help you create new contexts:

Method Concurrency Type Notes
+ [NSManagedObjectContext MR_newContext] NSPrivateQueueConcurrencyType Sets the default context as it's parent context.
+ [NSManagedObjectContext MR_newMainQueueContext] NSMainQueueConcurrencyType
+ [NSManagedObjectContext MR_newPrivateQueueContext] NSPrivateQueueConcurrencyType
+ [NSManagedObjectContext MR_newContextWithParent:…] NSPrivateQueueConcurrencyType Allows you to specify the parent context that will be set.
+ [NSManagedObjectContext MR_newContextWithStoreCoordinator:…] NSPrivateQueueConcurrencyType Allows you to specify the persistent store coordinator for the new context.

The Default Context

When working with Core Data, you will regularly deal with two main objects: NSManagedObject and NSManagedObjectContext.

MagicalRecord provides a simple class method to retrieve a default NSManagedObjectContext that can be used throughout your app. This context operates on the main thread, and is great for simple, single-threaded apps.

To access the default context, call:

NSManagedObjectContext *defaultContext = [NSManagedObjectContext MR_defaultContext];

This context will be used throughout MagicalRecord in any method that uses a context, but does not provde a specific managed object context parameter.

If you need to create a new managed object context for use in non-main threads, use the following method:

NSManagedObjectContext *myNewContext = [NSManagedObjectContext MR_newContext];

This will create a new managed object context which has the same object model and persistent store as the default context, but is safe for use on another thread. It automatically sets the default context as it's parent context.

If you'd like to make your myNewContext instance the default for all fetch requests, use the following class method:

[NSManagedObjectContext MR_setDefaultContext:myNewContext];

NOTE: It is highly recommended that the default context is created and set on the main thread using a managed object context with a concurrency type of NSMainQueueConcurrencyType.

Clone this wiki locally