Question

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 ?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top