Search code examples
objective-ccocoacore-datansmanagedobjectnsvaluetransformer

Using an NSValueTransformer to set values of an NSManagedObject instance


I am using a custom NSValueTransformer to store color information in my Core Data store. The transformation between Transformable data and a UIColor instance works great once the color data is in the store already (ie once the app has been run and quit once already). However when I first run the app and am loading in these values (from a text file) they "stuck" as NSCFStrings.

In this line of code "attributes" is a dictionary has keys which are NSManagedObject attribute names and values that are the expected values for those attributes. In my color example the key value pair is "color":"1,1,1,0.5"

[object setValue:[attributes valueForKey:attribute] forKey:attribute];

The value for "color" will now remain a string in this instance until it's get transformed via my NSValueTransformer and then retransformed into a UIColor when the app gets run again.

I could just do the same transform here that I'm doing in the NSValueTransformer, but this is in a utility class I wrote that could theoretically be used for any transformer. I also thought of finding a way to get all newly created NSManagedObject instances out fo memory thereby forcing the transformation to go through, but that just seems like a hack.

Note: This "hack" works for me and let's me continue, but still feels ugly. Use NSManagedObjectContext's reset method if you're having similar problems / looking for a "just work" solution.

Any ideas?

(I have a hunch this is similar to " Why is my transformable Core Data attribute not using my custom NSValueTransformer? " but outside of the title our problems seem to be different)

Here is my NSValueTransformer

@implementation UIColorRGBValueTransformer

+ (Class)transformedValueClass
{
    return [NSData class];
}

+ (BOOL)allowsReverseTransformation
{
    return YES;
}

- (id)transformedValue:(id)value
{
    return [value dataUsingEncoding:NSUTF8StringEncoding];
}

- (id)reverseTransformedValue:(id)value
{
    NSString *colorAsString = [[[NSString alloc] initWithData:value encoding:NSUTF8StringEncoding] autorelease];
    NSArray *components = [colorAsString componentsSeparatedByString:@","];
    CGFloat r = [[components objectAtIndex:0] floatValue];
    CGFloat g = [[components objectAtIndex:1] floatValue];
    CGFloat b = [[components objectAtIndex:2] floatValue];
    CGFloat a = [[components objectAtIndex:3] floatValue];
    return [UIColor colorWithRed:r green:g blue:b alpha:a];

    return nil;
}

@end

Solution

  • You're really not using the transformable attribute as intended. Your method should work in theory but it is an ugly kludge.

    You usually only have to write a custom value transformer for a system defined class when you want to do something non-standard. In the vast majority cases, the preferred method is to use the default NSKeyedUnarchiveFromDataTransformerName. Any class that implements the NSCoding protocol can use that value transformer.

    Since UIColor does implement the NSCoding protocol, you can just set the attribute to 'transformable' and the NSKeyedUnarchiveFromDataTransformerName will populate the transformer name automatically (at least it did in Xcode 3.x.) In use, Core Data will create the appropriate accessors so you can set and get the UIColor attribute just like any other key:

    [aMoObject setValue:aUIcolorObj forKey:@"colorAttributeName"];
    

    As a good rule of thumb, the API will do most of the work for you in the case of API classes. If you find yourself working hard with a system class, you've probably missed something.