更新CoreData中的NSDictionary属性不会保存 [英] Updates to NSDictionary attribute in CoreData not saving

查看:85
本文介绍了更新CoreData中的NSDictionary属性不会保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CoreData中创建了一个Entity,它包含一个实现为NSDictionary的Transformable属性类型。 NSDictionary属性只包含自定义类的值。自定义类的属性都是NSString类型。自定义类符合NSCoding实现:

I have created an Entity in CoreData that includes a Transformable attribute type implemented as an NSDictionary. The NSDictionary attribute only contains values of a custom class. The properties of the custom class are all of type NSString. The custom class complies with NSCoding implementing:

- (void)encodeWithCoder:(NSCoder *)coder;

-(void)encodeWithCoder:(NSCoder*)coder;

- (id)initWithCoder:(NSCoder *)coder

-(id)initWithCoder:(NSCoder *)coder

当保存Entity时,包括Transformable(NSDictionary)类型的所有属性都被正确保存在DB中。当从数据库提取相同的实体并更新(包括Transformable属性)时,它似乎被正确更新。但是,当应用程序关闭然后重新打开提取实体不显示更新的Transformable属性类型,尽管NSDate和NSString类型的其余属性是最新的。 Transformable属性是原始保存的值而不是更新的值。

When saving the Entity for the first time all attributes including the Transformable (NSDictionary) type are properly saved in the DB. When the same Entity is fetched from the DB and updated (including the Transformable attribute) it seems to be updated properly. However, when the app is closed and then reopened fetching the Entity does not show the updated Transformable attribute-type though the rest of the attributes of type NSDate and NSString are up-to-date. The Transformable attribute is the original saved value not the updated value.

这是KVO的一个问题,或者当我尝试保存一个NSDictionary类到CoreData?

Is this a problem with KVO or am I missing something else when trying to save an NSDictionary filled with a custom class to CoreData?

推荐答案

是否将值重新设置为 NSManagedObject NSManagedObject 不会监视可变对象的更改。您需要在保存之前调用相应的安装程序。

Are you setting the value back into the NSManagedObject? The NSManagedObject will not watch for changes to the transformable object. You need to call the appropriate setter before saving.

这篇关于更新CoreData中的NSDictionary属性不会保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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