Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data not saving changes to Transformable property

I am saving an NSMutableArray in a Transformable property in my Core Data store. I can create the entity properly with data in the NSMutableArray and then load it out of the property, and even make changes. When I go around my app and re-access it, my changes are saved. However when I reload the app, the changes have not saved in the core data store. Other changes to the entity - e.g. changing its title, which is saved as an NSString - are saved even when I quit and re-open the app.

I read in another StackOverflow question that Transformable attributes are not automatically notified of changes, and that I'd have to call "the appropriate setter before saving". However even using the actual setter functions - I have also tried calling didChangeValueForKey - the property is not saved properly. Any good ideas?

like image 328
Jason Avatar asked Jun 16 '10 21:06

Jason


People also ask

How to use Transformable attribute in Core Data?

To implement a Transformable attribute, configure it by setting its type to Transformable and specifying the transformer and custom class name in Data Model Inspector, then register a transformer with code before an app loads its Core Data stack.

How do I save an object in Core Data?

To save an object with Core Data, you can simply create a new instance of the NSManagedObject subclass and save the managed context. In the code above, we've created a new Person instance and saved it locally using Core Data.


1 Answers

You must, as you note, "re-set" a transformable property:

id temp = [myManagedObject myTransformableAttribute];

//.. do something with temp

[myManagedObject setMyTransformableAttribute:temp];

There is no way that Core Data could appropriately monitor an arbitrary transformable object so that it could 'do the right thing' automatically.

Furthermore, you must be certain that you actually save the managed object context after you modify the transformable attribute:

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

During a single run of the program, unsaved changes will appear visible because the managed object context keeps modified instances in memory. Without saving the context, however, those changes will not be persisted to disk.

like image 155
Barry Wark Avatar answered Sep 30 '22 12:09

Barry Wark