问题
If I understand correctly, the idea behind Core Data transformable attributes is:
- 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.
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 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
.
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