何时和为什么要使用多个NSManagedObjectContext? [英] when and why to use multiple NSManagedObjectContext?

查看:69
本文介绍了何时和为什么要使用多个NSManagedObjectContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我只使用一个moc在我的应用程序,但我想我应该使用多个NSManagedObjectContext在某些情况下。

Basically, I have only used one moc in my app, but I think I should use multiple NSManagedObjectContext in some cases.


  1. 我应该使用多个NSManagedObjectContext?

  2. 我听说我应该使用3 moc在某些情况下,但我不知道应该使用3个moc的哪些情况?


推荐答案

通常,您要为将访问Core Data db的每个线程使用单独的 NSManagedObjectContext 。这是为了防止对象图由于并发写入相同的上下文而潜在地进入不一致状态。

Typically, you want to use a separate NSManagedObjectContext for each thread that will access the Core Data db. This is to prevent the object graphs from potentially getting into an inconsistent state due to concurrent writes to the same context.

处理这个的最简单的方法是创建一个新的 NSManagedObjectContext 为每个线程共享一个 NSPersistentStoreCoordinator

The simplest way to handle this is to create a new NSManagedObjectContext for each thread and share a single NSPersistentStoreCoordinator.

在您的AppDelegate中创建一个属性 NSManagedObjectContext ,并重写getter为每个调用线程返回一个新的上下文。使用每个线程的 threadDictionary 来做到这一点。

Create a property in your AppDelegate of type NSManagedObjectContext and override the getter to return a new context for each calling thread. Do this by utilizing the threadDictionary of each thread.

首先,设置managedObjectModel和persistentStoreCoordinator 。然后在您的AppDelegate中创建上下文并分配给您的属性:

First, set up your managedObjectModel and persistentStoreCoordinator as you normally would. Then create your context in your AppDelegate and assign to your property:

self.managedObjectContext = [[NSManagedObjectContext alloc] init];
self.managedObjectContext.persistentStoreCoordinator = self.storeCoordinator;

在managedObjectContext getter覆盖中,使用以下代码为每个调用线程返回单独的上下文: / p>

In your managedObjectContext getter override, use the following code to return a separate context for each calling thread:

- (NSManagedObjectContext *) managedObjectContext
{
    NSThread *thisThread = [NSThread currentThread];
    if (thisThread == [NSThread mainThread])
    {
        //For the Main thread just return default context iVar      
        return _managedObjectContext;
    }
    else
    {
        //Return separate MOC for each new thread        
        NSManagedObjectContext *threadManagedObjectContext = [[thisThread threadDictionary] objectForKey:@"MOC_KEY"];
        if (threadManagedObjectContext == nil)
        {
            threadManagedObjectContext = [[[NSManagedObjectContext alloc] init];
            [threadManagedObjectContext setPersistentStoreCoordinator: [self storeCoordinator]];
            [[thisThread threadDictionary] setObject:threadManagedObjectContext forKey:@"MOC_KEY"];

        }

        return threadManagedObjectContext;
    }
}

现在在您访问managedObjectContext属性的代码中的AppDelegate,你一定是线程安全的。

Now anywhere in your code where you access the managedObjectContext property of the AppDelegate, you will be sure to be thread safe.

这篇关于何时和为什么要使用多个NSManagedObjectContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆