NSMutableArray存储核心数据= WORKS,但更改数组后不工作 [英] NSMutableArray stored with core data = WORKS, but after changing array DOESN'T WORK

查看:170
本文介绍了NSMutableArray存储核心数据= WORKS,但更改数组后不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSManagedObject与NSMutableArray作为属性:

I have a NSManagedObject with a NSMutableArray as attribute:

@interface MyObject :  NSManagedObject  
{
}

@property (nonatomic, retain) id a1;

在数据模型中,它被声明为 Transformable 。我离开了Value Transformer字段,就像它是默认的(浅灰色) NSKeyedUnarchiveFromData

In the data model it is declared as Transformable. I left the Value Transformer field like it is with the default (in light grey) NSKeyedUnarchiveFromData.

作为对象的一部分:

MyObject  *theObject = [NSEntityDescription insertNewObjectForEntityForName:@"MyObject" inManagedObjectContext: myManagedObjectContext];

并初始化:

a1 = [[NSMutableArray alloc] init];

使用将对象添加到a1中[a1 insertObject:[NSNumber numberWithInt:0 ] atIndex:0];

然后我保存上下文。加载上下文回存储在a1中的所有元素被保存和加载。 所有工作都很好!

Then I save the context after all that. Loading the context back all elements stored in a1 are saved and loaded. ALL WORKS WELL!

但是现在a1更改,例如通过向a1添加一个元素或更改任何元素在a1和上下文被保存和加载回来,a1的内容是UNCHANGED(它保持完全一样,所有的变化发生之前)。 更改不能正常工作!

BUT when now a1 CHANGES, for example by adding one more element to a1 or changing any element within a1 and the context is being saved and loaded back, the content of a1 is UNCHANGED (it remains exactly as it was before all changes happened). CHANGES DON'T WORK!

顺便说一下,当应用程序运行时,所有更改a1 ARE STORED在a1。

By the way, while the app is running, all changes to a1 ARE STORED in a1.

请帮助 - 这里是怎么回事?

Please, can you help - what is going on here?

帮帮我!

推荐答案

您的数组内部的更改将无法工作,因为Core Data无法查看数组。

Changes inside of your Array are not going to work because Core Data cannot see into the array.

简短的回答是不要这样做。这是没有理由在Core Data中存储数组(或字典)。

The short answer is don't do this. This is no reason ever to store an array (or dictionary for that matter) in Core Data.

在Core Data中创建一个新实体并创建一个关系。如果顺序很重要,请在子表中放置order属性。

Create a new entity in Core Data and create a relationship. If the order is important, put an order attribute in the child table.

不要将数组存储为二进制对象。

Do not store arrays as binary objects.

这篇关于NSMutableArray存储核心数据= WORKS,但更改数组后不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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