Core Data not automatically calling value transformer when getting / setting attribute directly in code

佐手、 提交于 2020-01-04 09:42:13

问题


If I understand correctly, the idea behind Core Data transformable attributes is:

  1. implement an NSValueTransformer subclass with returns [NSData class] in +transformedValueClass along with its implementation for transformation
  2. register the transformer in +load or +initialize
  3. set an entity's attribute as transformable
  4. set a name for your transformer (the name you used to register it) in the xcode model editor for the attribute.

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 - 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 to NSData 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 from NSString to NSData and neither does NSString *myString = myManagedObject.myAttribute trigger the conversion from NSData to NSString.

So what am I missing? I thought the idea was that CoreData would automatically call the transformer. Am I wrong?

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 ?


回答1:


(From the comment above:) The inverse transformer is called when you save the context, not when you set an attribute.



来源:https://stackoverflow.com/questions/15861892/core-data-not-automatically-calling-value-transformer-when-getting-setting-att

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!