Core Data不保存对Transformable属性的更改 [英] Core Data not saving changes to Transformable property

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

问题描述

我在Core Data商店的Transformable属性中保存了一个 NSMutableArray 。我可以使用 NSMutableArray 中的数据正确创建实体,然后将其加载到属性中,甚至进行更改。当我浏览我的应用并重新访问它时,我的更改会被保存。但是,当我重新加载应用程序时,更改尚未保存在核心数据存储中。对实体的其他更改 - 例如更改其标题,保存为 NSString - 即使在我退出并重新打开应用程序时也会保存。

I am saving an NSMutableArray in a Transformable property in my Core Data store. I can create the entity properly with data in the NSMutableArray and then load it out of the property, and even make changes. When I go around my app and re-access it, my changes are saved. However when I reload the app, the changes have not saved in the core data store. Other changes to the entity - e.g. changing its title, which is saved as an NSString - are saved even when I quit and re-open the app.

我在另一个StackOverflow问题中读到,Transformable属性不会自动通知更改,我必须在保存前称之为合适的二传手。然而,即使使用实际的setter函数 - 我也尝试调用 didChangeValueForKey - 该属性未正确保存。有什么好主意吗?

I read in another StackOverflow question that Transformable attributes are not automatically notified of changes, and that I'd have to call "the appropriate setter before saving". However even using the actual setter functions - I have also tried calling didChangeValueForKey - the property is not saved properly. Any good ideas?

推荐答案

正如你所说,你必须重新设定一个可转换的财产:

You must, as you note, "re-set" a transformable property:

id temp = [myManagedObject myTransformableAttribute];

//.. do something with temp

[myManagedObject setMyTransformableAttribute:temp];

Core Data无法正确监控任意可转换对象,以便它可以执行正确的事情'自动。

There is no way that Core Data could appropriately monitor an arbitrary transformable object so that it could 'do the right thing' automatically.

此外,您必须确定在修改可转换属性后实际保存了托管对象上下文:

Furthermore, you must be certain that you actually save the managed object context after you modify the transformable attribute:

NSError *error;
if(![[myManagedObject managedObjectContext] save:&error]) {
  //present error
}

在单次运行程序期间,未保存的更改将显示为可见,因为托管对象上下文将修改后的实例保留在内存中。但是,如果不保存上下文,这些更改将不会保留到磁盘。

During a single run of the program, unsaved changes will appear visible because the managed object context keeps modified instances in memory. Without saving the context, however, those changes will not be persisted to disk.

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

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