xcode NSMutableArray 与核心数据一起存储 = WORKS,但在更改数组后不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3426557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:36:56  来源:igfitidea点击:

NSMutableArray stored with core data = WORKS, but after changing array DOESN'T WORK

xcodecore-datansmutablearray

提问by user387184

I have a NSManagedObject with a NSMutableArray as attribute:

我有一个带有 NSMutableArray 作为属性的 NSManagedObject:

@interface MyObject :  NSManagedObject  
{
}

@property (nonatomic, retain) id a1;

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.

在数据模型中,它被声明为Transformable. 我将 Value Transformer 字段保留为默认值(浅灰色)NSKeyedUnarchiveFromData

a1 is created as part of theObject:

a1 作为对象的一部分创建:

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

and initialized:

并初始化:

a1 = [[NSMutableArray alloc] init];

Objects are added to a1 with [a1 insertObject:[NSNumber numberWithInt:0] atIndex: 0 ];

对象被添加到 a1 [a1 insertObject:[NSNumber numberWithInt:0] atIndex: 0 ];

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 中的所有元素都被保存和加载。ALL WORKS WELL!

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 发生变化时,例如通过向 a1 添加一个更多元素或更改 a1 中的任何元素并且上下文正在保存和加载,a1 的内容是未更改的(它保持所有更改发生之前的状态)。 CHANGES DON'T WORK!

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

顺便说一下,当应用程序运行时,对 a1 的所有更改都存储在 a1 中。

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

拜托,你能帮忙吗 - 这里发生了什么?

Thanks ver much for your help!

非常感谢您的帮助!

回答by Marcus S. Zarra

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

数组内部的更改将不起作用,因为核心数据无法查看数组。

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 中存储数组(或字典)的理由。

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

在 Core Data 中创建一个新实体并创建关系。如果订单很重要,则在子表中放置一个订单属性。

Do not store arrays as binary objects.

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