Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use multiple managed objects context with core data base

I am using core data to store and fetch my data but I am facing some issue. I want to use two thread parallel for following operations:

  1. Thread one will insert data in CoreData base table(A).
  2. Thread two will fetch data from another table(B).

How I can do that?

I did some research on google and they said, we need to use multiple managed object context, But I don't know how we will use that.

like image 539
Riyansh Avatar asked Apr 07 '15 10:04

Riyansh


People also ask

Can we have multiple managed object context in Core Data?

Most apps need just a single managed object context. The default configuration in most Core Data apps is a single managed object context associated with the main queue. Multiple managed object contexts make your apps harder to debug; it's not something you'd use in every app, in every situation.

Will you ever pass a managed object from one context to another context?

You cannot pass NSManagedObjects between multiple contexts, but you can pass NSManagedObjectIDs and use them to query the appropriate context for the object represented by that ID.

What is managed object context in Core Data?

A managed object context represents a single object space, or scratch pad, in a Core Data application. A managed object context is an instance of NSManagedObjectContext . Its primary responsibility is to manage a collection of managed objects.

Can we use Core Data managed objects from background thread?

There are multiple ways to use Core Data and a NSManagedObjectContext on a background thread. One way is the performBackgroundTask(_:) method on an NSPersistentContainer : // Spawns a new thread and creates a background // managed object context to perform operations // in the background persistentContainer.


1 Answers

You should not access your NSManagedObjectContext on multiple threads. The NSManagedObjectContext created in your AppDelegate should only be accessed on main thread.

It implies, you should create a NSManagedObjectContext for each thread you create. Make sure to set the thread's NSManagedObjectContext's parent context as your main context.

Example : -

NSManagedObjectContext *mainContext; // = getMainContext
NSManagedObjectContext *threadContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
threadContext.parentContext = mainContext;

and then use threadContext on your thread...

You can continue your UI related fetching on main thread. Or if it is essential to have other thread for it too, create a context for it too.

To know the Core Data concurrency in depth see a tutorial

Setting Parent/Child context relationship will merge your thread's Context with main context (it's parent context).

To understand Parent/Child context relationship check this URL

Or just under this diagram -

Parent/Child context relationship.

Credits to the article URL...

like image 131
Mayur Deshmukh Avatar answered Sep 19 '22 12:09

Mayur Deshmukh