CoreData 中 NSDictionary 属性的更新未保存
我在 CoreData 中创建了一个实体,其中包含作为 NSDictionary 实现的 Transformable 属性类型。 NSDictionary 属性仅包含自定义类的值。自定义类的属性都是NSString类型。自定义类符合 NSCoding 实现:
-(void)encodeWithCoder:(NSCoder*)coder;
-(id)initWithCoder:(NSCoder *)coder
第一次保存实体时,包括 Transformable (NSDictionary) 类型在内的所有属性都会正确保存在数据库中。当从数据库获取并更新相同的实体(包括 Transformable 属性)时,它似乎已正确更新。但是,当应用程序关闭然后重新打开时,获取实体不会显示更新的 Transformable 属性类型,尽管 NSDate 和 NSString 类型的其余属性是最新的。 Transformable 属性是原始保存的值而不是更新后的值。
这是 KVO 的问题还是我在尝试将填充有自定义类的 NSDictionary 保存到 CoreData 时遗漏了其他内容?
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;
-(id)initWithCoder:(NSCoder *)coder
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否将该值设置回 NSManagedObject 中?
NSManagedObject
将不会监视可变形对象的更改。您需要在保存之前调用适当的设置器。Are you setting the value back into the
NSManagedObject
? TheNSManagedObject
will not watch for changes to the transformable object. You need to call the appropriate setter before saving.我遇到了同样的问题,最终切换到 NSDictionary 作为可转换属性,而不是 NSMutableDictionary。只需将 NSDictionary 作为 mutableCopy 获取,然后进行处理,将最终结果放入 NSDictionary 中,然后将其重新插入到 ManagedObject 中。
对我有用,但我还没有找到其他解决方案。
I ran into the same problem and ended up switching to NSDictionary as transformable attribute instead of NSMutableDictionary. Just fetch the NSDictionary as mutableCopy, work on that, put the end result into an NSDictionary and reinsert that into the managedObject.
Did the trick for me and i havent found another solution yet.