NSPrivateQueueConcurrencyType

NSPrivateQueueConcurrencyType

本文介绍了多上下文CoreData与线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UPDATE:我想问题是父上下文在子上下文保存时不会更新。仍然需要帮助。

UPDATE : I suppose the issue is that the parent context is not updated when the child context is saved. Still, need help.

我已经尝试过多个上下文(父子)核心数据的例子。

I have tried many examples of Multi-context (Parent-Child) Core Data.

以前,我的应用程序使用传统的存储数据的方式,即我使用OperationQueue,其中从服务器获取数据并使用MOC保存到数据库保存到mainMOC的通知mergeChanges:NSManagedObjectContextDidSaveNotification。

Previously my app was using the traditional way of storing data, i.e. I used an OperationQueue where I fetch the data from the server and save to the DB using a MOC and on saving fire a notification to the mainMOC to mergeChanges : NSManagedObjectContextDidSaveNotification.

在不打扰应用程序的流程,(即删除OperationQueue),我试图实现父子ManagedObjectContext关系,其中我使用了concurrencyType为 NSPrivateQueueConcurrencyType 的privateMOC,它有一个persistantStoreCoordinator,mainMOC的concurrenyType为 NSMainQueueConcurrencyType 是privateMOC的孩子。在Queue中,我有一个tempMOC,并且concurrencyType为 NSPrivateQueueConcurrencyType ,它是mainMOC的子对象。

Without disturbing the flow of the app, (i.e. removing the OperationQueue), I tried to implement the Parent-Child ManagedObjectContext Relationship where I used a privateMOC with concurrencyType as NSPrivateQueueConcurrencyType which has a persistantStoreCoordinator, and the mainMOC with concurrenyType as NSMainQueueConcurrencyType which is a child of the privateMOC. And in the Queue I have a tempMOC with concurrencyType as NSPrivateQueueConcurrencyType which is a child of the mainMOC.

,我将三个MOC的 performBlock 嵌套为 -

While saving, I nest the performBlock of the three MOCs as -

[tempMOC performBlock:^{
        if (![tempMOC save:&error]) {
            NSLog(@"Error : %@",error);
        }
        [mainMOC performBlock:^{
            if (![mainMOC save:&error]) {
                NSLog(@"Error : %@",error);
            }
            [privateMOC performBlock:^{
                if (![privateMOC save:&error]) {
                    NSLog(@"Error : %@",error);
                }
            }];
        }];
    }];

在mainMOC尝试保存时,我收到类似CoreData 1560和1570的错误。 NSValidationErrorKey 错误,其中一些值是 nil
是不是tempMOC的更改不会到mainMOC?我没有挖,但据我所知,它不应该是零。
可能是错误?请帮助。

I am getting Errors like CoreData 1560 and 1570 while the mainMOC is trying to save. NSValidationErrorKeyerror where it says some value is nil.Is it that the changes of the tempMOC are not going to the mainMOC ? I did not dig in but as far as I know, it should not be nil.What could possibly be the error? Please Help.

更新:我尝试打印tempMOC的对象,我看到正确的值如下:

UPDATE : I tried to print the objects of tempMOC and I see proper values like :

<Element_Name: 0xc0b59c0> (entity: Element_Name; id: 0xc07ca90 <x-coredata:///Element_Name/t2DCD57A8-4C1A-4AF7-A10E-5B9603E2BB8730> ; data: {
    tag1 = nil;
    tag2 = 3430065;
    tag3 = 600;
    tag4 = N;
    tag5 = "2013-10-29 00:00:00 +0000";
    tag6 = nil;
    tag7 = 327842701;
    relation = "0xbf1f760 <x-coredata://87C54A94-701E-4108-826E-4D98A53380F9/Relation/p1>";
    tag8 = "Some_Value";

我尝试打印mainMOC的对象,我看到 nil

I tried to print the objects of mainMOC and I see nil value instead of the data like :

<Element_Name: 0xbd47a50> (entity: Element_name; id: 0xc0b14b0 <x-coredata:///Element_Name/t2DCD57A8-4C1A-4AF7-A10E-5B9603E2BB8740> ; data: {
    tag1 = nil;
    tag2 = nil;
    tag3 = 0;
    tag4 = nil;
    tag5 = nil;
    tag6 = nil;
    tag7 = nil;
    relation = "0xbd586c0 <x-coredata://87C54A94-701E-4108-826E-4D98A53380F9/relation/p1>";
    tag8 = nil;


推荐答案

我刚刚遇到同样的问题,没有你的代码的其余部分,我不能保证这将解决你的问题,但它确实解决了我。

I just hit the same problem and found a solution. Without the rest of your code I cannot assure this would solve your problem, but it did solve mine.

我实例化一些NSManagedObject类,修改一些属性和THEN将它们插入到临时或子 NSManagedObjectContext 中。

I was instantiating some NSManagedObject classes, modifying some of their properties and THEN inserting them in the temp or child NSManagedObjectContext. All the properties were showing just fine like in your case.

但是当我保存这个上下文并且更改被推送到父 NSManagedObjectContext ,所有属性都被取消(像你的情况)。

But when I saved that context and the changes got pushed to the parent NSManagedObjectContext, all properties were nullified (like in your case).

我没有观察到这个行为当只使用一个 NSManagedObjectContext ,我没有尝试过老的 NSManagedObjectContextDidSaveNotification 模式。

I have not observersed this behaviour when using only one NSManagedObjectContext, and I have not experimented with the older NSManagedObjectContextDidSaveNotification pattern.

解决方案是当然,在初始化之后,在任何属性赋值完成之前,将 NSManagedObject 添加到上下文中。

The solution is of course to add the NSManagedObject to a context just after initialisation, and before any property assignments are done.

这篇关于多上下文CoreData与线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:49