Core Data, Bindings, value transformers : crash when saving
I am trying to store a PNG image in a core data store backed by an sqlite database. Since I intend to use this database on an iPhone I can't store NSImage
objects directly. I wanted to use bindings and an NSValueTransformer
subclass to handle the transc开发者_Go百科oding from the NSImage
(obtained by an Image well on my GUI) to an NSData
containing the PNG binary representation of the image. I wrote the following code for the NSValueTransformer
:
+ (Class)transformedValueClass
{
return [NSImage class];
}
+ (BOOL)allowsReverseTransformation
{
return YES;
}
- (id)transformedValue:(id)value
{
if (value == nil) return nil;
return [[[NSImage alloc] initWithData:value] autorelease];
}
- (id)reverseTransformedValue:(id)value
{
if (value == nil) return nil;
if(![value isKindOfClass:[NSImage class]])
{
NSLog(@"Type mismatch. Expecting NSImage");
}
NSBitmapImageRep *bits = [[value representations] objectAtIndex: 0];
NSData *data = [bits representationUsingType:NSPNGFileType properties:nil];
return data;
}
The model has a transformable property configured with this NSValueTransformer
. In Interface Builder a table column and an image well are both bound to this property and both have the proper value transformer name (an image dropped in the image well shows up in the table column). The transformer is registered and called every time an image is added or a row is reloaded (checked with NSLog()
calls). The problem arises when I am trying to save the managed objects. The console output shows the error message:
[NSImage length]: unrecognized selector sent to instance 0x1004933a0
It seems like core data is using the value transformer to obtain the NSImage
back from the NSData
and then tries to save the NSImage
instead of the NSData
.
There are probably workarounds such as the one presented in this post but I would really like to understand why my approach is flawn.
Thanks in advance for your ideas and explanations.
I eventually got it to work (best to sleep on it). There were two problems. The first one was that I specified the same value transformer both in IB bindings and in the core data model and it was redundant.
My understanding is that IB bindings specified value transformers actually transform values between the views and the controllers whereas the core data value transformer acts as a middleman between the managed objects and the persistent store.
To make the matter worse, the second problem was that my value transformer was upside down. The transformValue
message is sent when the objects are saved (managed object -> store) and the reverseTransformedValue
is sent when loading the values from the store which is somewhat the opposite direction of the transformers used when binding view objects to model objects. I got the hint from the core data programming guide where you can read the following somewhat cryptic note :
Important: Although the default transformer is the transformer specified by NSKeyedUnarchiveFromDataTransformerName, this transformer is actually used in reverse. If you specify the default transformer explicitly, Core Data would use it “in the wrong direction.”
Even though my program is working so far, I would like to have confirmation from someone with core data experience and I would appreciate if you could point me to some documentation on that matter that I might have missed.
Thanks in advance.
The working code :
+ (Class)transformedValueClass
{
return [NSData class];
}
+ (BOOL)allowsReverseTransformation
{
return YES;
}
- (id)reverseTransformedValue:(id)value
{
if (value == nil) return nil;
return [[[NSImage alloc] initWithData:value] autorelease];
}
- (id)transformedValue:(id)value
{
if (value == nil) return nil;
if(![value isKindOfClass:[NSImage class]])
{
NSLog(@"Type mismatch. Expecting NSImage");
}
NSBitmapImageRep *bits = [[value representations] objectAtIndex: 0];
NSData *data = [bits representationUsingType:NSPNGFileType properties:nil];
return data;
}
In transformedValue: you go straight from NSData to NSImage but in reverseTransformedValue: you go from NSImage to NSBitmapImageRep to NSData. So the two don't work together because they are using different methodologies. The data in your model is actually NSBitmapImageRep data and not NSImage data. The two need to be exact opposites of each other.
By the way, I posted a transformer for this recently in another post. You can see it here. And of course you don't need a custom transformer at all as you could just use NSKeyedArchiver. NSImage adheres to the NSCoder protocol so it would handle the transformation for you.
精彩评论