核心数据不更新可转换属性 [英] Core Data not updating a transformable attribute

查看:113
本文介绍了核心数据不更新可转换属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Core Data NSManagedObject(在iOS应用程式中)使用类型为transformable的属性来储存大量资料。使用内置的NSKeyedUnarchiveFromData值变换器对数据进行编码/解码。问题是,我有麻烦让NSManagedObject在二进制数据更改后正确更新。例如,代码如下:

I am using a Core Data NSManagedObject (in an iOS app) with an attribute of type "transformable" to store a chunk of data. The data is encoded/decoded using the built-in NSKeyedUnarchiveFromData value transformer. The problem is that I'm having trouble getting the NSManagedObject to update properly after the binary data has changed. Say, for example, the code looks like:

id temp = [myManagedObject myTransformableAttribute];

//.. do something with temp

[myManagedObject setMyTransformableAttribute:temp];

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

在我看来,将可变属性重新设置到托管对象,然后保存托管对象,应该导致数据通过NSKeyedUnarchiveFromData值重新编码变压器。但是编码器从不被调用,更新的数据不会保存。

It seems to me that "re-setting" the transformable attribute onto the managed object, and then saving the managed object, should cause the data to be re-encoded via the NSKeyedUnarchiveFromData value transformer. But the encoder never gets called, and the updated data doesn't get saved.

如果不是将原始数据重新设置回托管对象,我创建一个复制修改的数据并将 置于托管对象上,然后识别变化。核心数据是否可能使用缓存版本的数据?还是有其他我在这里做错了吗?谢谢...

If instead of re-setting the original data back onto the managed object, I create a copy of the modified data and set that onto the managed object, then the changes are recognized. Is it possible that core data is using a cached version of the data? Or is there something else I'm doing wrong here? Thanks...

推荐答案

看起来你需要在NSManagedObject实体子类中重写Class initialize方法,数据错误)。
以下代码来自Apple的位置代码示例,它的测试和工作原理如下:
http://developer.apple.com/library/ios/#samplecode/Locations/Introduction/Intro.html

Seems you need to override Class initialize method in your NSManagedObject entity subclass also for transformer to work (known Core Data bug). Following code is from Apple's location code sample, it is tested and works: http://developer.apple.com/library/ios/#samplecode/Locations/Introduction/Intro.html

+ (void)initialize {
    if (self == [Event class]) {
        UIImageToDataTransformer *transformer = [[UIImageToDataTransformer alloc] init];
        [NSValueTransformer setValueTransformer:transformer forName:@"UIImageToDataTransformer"];
    }
}

这篇关于核心数据不更新可转换属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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