Core Data在代码中直接获取/设置属性时不自动调用值变换器 [英] Core Data not automatically calling value transformer when getting / setting attribute directly in code
问题描述
如果我理解正确,Core Data可变属性背后的想法是:
If I understand correctly, the idea behind Core Data transformable attributes is:
- 实现
NSValueTransformer
在
+ transformedValueClass
中返回[NSData
的子类b $ b进行转换
class] - 在
+ load
或+ initialize
- 将实体的属性设置为可变形
- 设置变换器的名称(用于注册它的名称)
xcode模型编辑器。
- implement an
NSValueTransformer
subclass with returns[NSData class]
in+transformedValueClass
along with its implementation for transformation - register the transformer in
+load
or+initialize
- set an entity's attribute as transformable
- set a name for your transformer (the name you used to register it) in the xcode model editor for the attribute.
此时,我期望访问或设置managedObject中的属性实体类型将触发值变换器。但是,我在使用 AFIncrementalStore
的应用程序中进行测试,并得到以下行为:
At this point, I'd expect that accessing or setting the attribute in a managedObject of the appropriate entity type would trigger the value transformer. However, I'm testing this in an app that uses AFIncrementalStore
and I get the following behavior:
- A - 在
+ load
或+初始化
中注册变压器不会
必要; - B - 通过
AFIncrementalStore
获取请求将触发
变压器。例如,当将响应字典映射到managedObject时,我从获取请求和
获取JSON,触发
变换器并覆盖适当的字典键
到 - C - HOWEVER,如果我尝试通过代码设置或获取属性,
变换器不被调用。这是像
myManagedObject.myAttribute = @hello
不会触发
从NSString
到NSData
,也不会NSString
* myString = myManagedObject.myAttributeNSData
至NSString
。
- A - registering the transformer in
+load
or+initialize
doesn't seem necessary; Core Data finds it anyway (though read ahead). - B - fetch requests via
AFIncrementalStore
do trigger the transformer. For example, I get JSON back from a fetch request and when mapping the response dictionary to the managedObject, the transformer is triggered and coverts the appropriate dictionary key toNSData
in the object. - C - HOWEVER, if I try to set or get the attribute via code, the
transformer is not called. That is doing something like
myManagedObject.myAttribute = @"hello"
does not trigger the conversion fromNSString
toNSData
and neither doesNSString *myString = myManagedObject.myAttribute
trigger the conversion fromNSData
toNSString
.
那么我缺少什么?我想的想法是CoreData会自动调用变压器。我错了吗?
So what am I missing? I thought the idea was that CoreData would automatically call the transformer. Am I wrong?
根据这个问题:为什么我的可变核心数据属性不使用我的自定义NSValueTransformer?
这似乎是苹果框架中的一个错误。但是引发我的是,通过AFIncrementalStore值变换器被调用。也许关键是,通过设置只是一个属性通过代码我不真正触发AFIncrementalStore,所以改变只是在内存中。
According to this question: Why is my transformable Core Data attribute not using my custom NSValueTransformer? this seems to be a bug in the Apple frameworks. But what throws me off is that via AFIncrementalStore the value transformer does get called. Maybe the key is that by setting just an attribute via code I am not really triggering AFIncrementalStore and so the change is merely in-memory ?
推荐答案
(从上面的注释):当您保存上下文时调用逆变换,
不是在设置属性时调用。
(From the comment above:) The inverse transformer is called when you save the context, not when you set an attribute.
这篇关于Core Data在代码中直接获取/设置属性时不自动调用值变换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!