views:

54

answers:

2

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?

+1  A: 

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.

Barry Wark
I've experienced this issue with NSMutableArrays as the transformable attribute, and I was accessing the appropriate setter methods and saving the context properly (other attributes were getting saved as proof). In my case the solution was to change the type to an NSArray.
JoBu1324
A: 

I am having a similar issue.

I have a core data object, with a few attributes and a to-many relationship. I use this to hold state when the app is quit.

The to-many relationship holds a list of questions (other core-data objects). When the application boots, it loads this list into an NSArray. This NSArray might change. So when the application terminates, I set the relationship to the contents of the array, using the proper setter method. Then send the managed context a save message.

After this point if I access relationship, all is fine. However, if I quit, then re-run the app, the list has the old set plus any new members of NSArray. Its like I performed a add...:(NSSet *) rather than a set...:(NSSet *) call.

This is driving me crazy - any ideas?