I have an application, which stores the data in Core Data Managed Objects. These objects contain several properties – information as NSString, Boolean as NSNumber and an data array as Transformable.
dataPoint.h:
@property (nonatomic, retain) id dataArray;
@property (nonatomic, retain) NSString * note;
@property (nonatomic, retain) NSNumber * bool;
dataPoint.m:
@dynamic dataArray;
@dynamic note;
@dynamic bool;
The application user can make changes and store or cancel them. To undo all changes I call the [managedObjectContest rollback] method. I haven’t set up an additional undoManager.
For simple data types (like NSString or NSNumber) this works fine. After the rollback is called, all entries are in there last saved state. But this seems not to work for the stored Array. After rollback is called, I still only get the changed Values. Only after I restart the application, the original (last saved) data is shown.
What do I have to do to make the rollback work for complex data?
Thank you and kind regards!
You could simply re-fetch the entity after the rollback. In this way, the data from the persistent store (presumably the correct data) would replace the changed array.
Another approach would be to just keep a copy of the old array and use that in displaying your data, if this is feasible (maybe not across multiple view controllers).
An array as a property is a rather non-standard way of going about storing things in NSManagedObjects. The phenomenon happens because once you retrieve the array, you essentially get a pointer to it. You change the array, but the pointer stays the same. During rollback nothing has to be changed, so Core Data just does nothing. Your array in memory is not relevant for Core Data.
It would be much better if you took the trouble and model the things in your array also in Core Data. It is likely to results in more explicit, readable and maintainable code, in addition to have a more solid data structure.