Why is my transformable Core Data attribute not using my custom NSValueTransformer?

I have a Core Data app with a fairly simple data model. I want to be able to store instances of NSImage in the persistent store as PNG Bitmap NSData objects, to save space.

To this end, I wrote a simple NSValueTransformer to convert an NSImage to NSData in PNG bitmap format. I am registering the value transformer with this code in my App delegate:

  • Why might I want 2 or more Core Data models?
  • Does @synchronized guarantees for thread safety or not?
  • Synchronization/wait design for cross-thread event signaling (Obj-C)?
  • What is an easy way to break an NSArray with 4000+ objects in it into multiple arrays with 30 objects each?
  • Core data structure use multiple entities or not?
  • How to list variables for NSManagedObject
  • + (void)initialize
    {
        [NSValueTransformer setValueTransformer:[[PNGDataValueTransformer alloc] init] forName:@"PNGDataValueTransformer"];
    }
    

    In my data model, I have set the image attribute to be Transformable, and specified PNGDataValueTransformer as the value transformer name.

    However, my custom value transformer is not being used. I know this as I have placed log messages in my value transformer’s -transformedValue: and -reverseTransformedValue methods which are not being logged, and the data that is being saved to disk is just an archived NSImage, not the PNG NSData object that it should be.

    Why is this not working?

    Here is the code of my value transformer:

    @implementation PNGDataValueTransformer
    
    + (Class)transformedValueClass
    {
        return [NSImage class];
    }
    
    + (BOOL)allowsReverseTransformation
    {
        return YES;
    }
    
    - (id)transformedValue:(id)value
    {
        if (value == nil) return nil;
        if(NSIsControllerMarker(value))
            return value;
        //check if the value is NSData
        if(![value isKindOfClass:[NSData class]])
        {
            [NSException raise:NSInternalInconsistencyException format:@"Value (%@) is not an NSData instance", [value class]];
        }
        return [[[NSImage alloc] initWithData:value] autorelease];
    }
    
    - (id)reverseTransformedValue:(id)value;
    {
        if (value == nil) return nil;
        if(NSIsControllerMarker(value))
            return value;
        //check if the value is an NSImage
        if(![value isKindOfClass:[NSImage class]])
        {
            [NSException raise:NSInternalInconsistencyException format:@"Value (%@) is not an NSImage instance", [value class]];
        }
        // convert the NSImage into a raster representation.
        NSBitmapImageRep* bitmap    = [NSBitmapImageRep imageRepWithData: [(NSImage*) value TIFFRepresentation]];
        // convert the bitmap raster representation into a PNG data stream
        NSDictionary* pngProperties = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:NSImageInterlaced];
        // return the png encoded data
        NSData* pngData             = [bitmap representationUsingType:NSPNGFileType properties:pngProperties];
        return pngData;
    }
    
    @end
    

    6 Solutions Collect From Internet About “Why is my transformable Core Data attribute not using my custom NSValueTransformer?”

    If I’m not mistaken, your value transformer has a reversed direction. I do the same thing in my application, using code like the following:

    + (Class)transformedValueClass 
    {
     return [NSData class]; 
    }
    
    + (BOOL)allowsReverseTransformation 
    {
     return YES; 
    }
    
    - (id)transformedValue:(id)value 
    {
     if (value == nil)
      return nil;
    
     // I pass in raw data when generating the image, save that directly to the database
     if ([value isKindOfClass:[NSData class]])
      return value;
    
     return UIImagePNGRepresentation((UIImage *)value);
    }
    
    - (id)reverseTransformedValue:(id)value
    {
     return [UIImage imageWithData:(NSData *)value];
    }
    

    While this is for the iPhone, you should be able to swap in your NSImage code at the appropriate locations. I simply haven’t tested my Mac implementation yet.

    All I did to enable this was to set my image property to be transformable within the data model, and specify the name of the transformer. I didn’t need to manually register the value transformer, as you do in your +initialize method.

    It seems registering the transformer has no effect on wether Core Data will use it or not.
    I’ve been playing with the PhotoLocations sample code and removing the transformer registration has no effect. As long as your ValueTransformerName is the name of your class, and that your transformer’s implementation is included with the target, it should work.

    If there is no code execution in the transformer, then the code in the transformer is irrelevant. The problem must be somewhere else in the core data stack, or in the declaration of the transformer.

    It turns out that this is actually a bug in the frameworks. See this post by an Apple employee on the Cocoa-Dev mailing list:

    http://lists.apple.com/archives/Cocoa-dev/2009/Dec/msg00979.html

    You need to explicitly register your transformer during runtime.

    Good place to do this is to override Class initialize method in your NSManagedObject entity subclass. As mentioned before it is known Core Data bug. Following is the crucial code 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"];
        }
    }
    

    If nothing in your code elsewhere explicity uses the PNGDataValueTransformer class, then the +initialize method for that class will never be called. Specifying the name in your Core Data model will not trigger it either – it will simply try looking up a value transformer for that name, which will return nil, since no transformer instance has yet been registered under that name.

    If this is indeed what’s happening in your case, simply add a call to [PNGDataValueTransformer initialize] somewhere in your code before your data model gets accessed, e.g. in the +initialize method of whatever class it is that’s using this data model. That should trigger the value transformer instance being created and registered so that Core Data can access it when it needs to.

    Check out the example code here.

    Does this do what you want?

    @implementation UIImageToDataTransformer
    
    
    + (BOOL)allowsReverseTransformation {
        return YES;
    }
    
    + (Class)transformedValueClass {
        return [NSData class];
    }
    
    
    - (id)transformedValue:(id)value {
        NSData *data = UIImagePNGRepresentation(value);
        return data;
    }
    
    
    - (id)reverseTransformedValue:(id)value {
        UIImage *uiImage = [[UIImage alloc] initWithData:value];
        return [uiImage autorelease];
    }