grfryling
grfryling

Reputation: 802

Core Data not updating a transformable attribute

I am using a Core Data NSManagedObject (in an iOS app) with an attribute of type "transformable" to store a chunk of data. The data is encoded/decoded using the built-in NSKeyedUnarchiveFromData value transformer. The problem is that I'm having trouble getting the NSManagedObject to update properly after the binary data has changed. Say, for example, the code looks like:

id temp = [myManagedObject myTransformableAttribute];

//.. do something with temp

[myManagedObject setMyTransformableAttribute:temp];

NSError *error;
if(![[myManagedObject managedObjectContext] save:&error]) {
    //present error
}

It seems to me that "re-setting" the transformable attribute onto the managed object, and then saving the managed object, should cause the data to be re-encoded via the NSKeyedUnarchiveFromData value transformer. But the encoder never gets called, and the updated data doesn't get saved.

If instead of re-setting the original data back onto the managed object, I create a copy of the modified data and set that onto the managed object, then the changes are recognized. Is it possible that core data is using a cached version of the data? Or is there something else I'm doing wrong here? Thanks...

Upvotes: 3

Views: 1599

Answers (2)

Lukasz
Lukasz

Reputation: 19916

Seems you need to override Class initialize method in your NSManagedObject entity subclass also for transformer to work (known Core Data bug). Following code is 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"];
    }
}

Upvotes: 2

sunshineDev
sunshineDev

Reputation: 321

I just ran into this same problem and apparently it seems to be a known bug:

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

Upvotes: 0

Related Questions