Undo core data managed object

后端 未结 5 2128
野性不改
野性不改 2021-02-06 15:26

I have this code:

Store* store = [NSEntityDescription insertNewObjectForEntityForName:@\"Store\"];
store.name = @\"My Company\"
...

Now the sto

相关标签:
5条回答
  • 2021-02-06 15:47

    Also you could save all data from the user in an array and when the user is ready, you only have to save the array to core data.

    0 讨论(0)
  • 2021-02-06 15:54

    Undo works only when I create a undoManager(Swift 5):

    managedObjectContext.undoManager = UndoManager()
    

    After this configuration you can undo a last change:

    managedObjectContext.undo()
    
    0 讨论(0)
  • 2021-02-06 15:57

    Core Data has built-in support for undo, so you can undo individual changes by sending the -undo message to the context:

    [store.managedObjectContext undo];
    

    It also supports -redo. You can undo all changes up to the most recent save using the -rollback method:

    [store.managedObjectContext rollback]
    

    as indicated in @melsam's answer.

    0 讨论(0)
  • 2021-02-06 16:07

    As mentioned earlier, you can use an undo manager. Or, you could simply use a separate ManagedObjectContext, and do all your changes in there. If you decide to keep them, save the context. If not, simply discard it. A MOC is just a scratch pad for work, and has no impact on the underlying database until saved.

    You can't really "detach an entity" but you can cause a managed object to turn back into a fault, losing any changes that have not been saved.

    [managedObjectContext refreshObject:object mergeChanges:NO];
    

    Snipped from the documentation...

    If flag is NO, then object is turned into a fault and any pending changes are lost. The object remains a fault until it is accessed again, at which time its property values will be reloaded from the store or last cached state.

    0 讨论(0)
  • 2021-02-06 16:08
    [store.managedObjectContext rollback];
    
    0 讨论(0)
提交回复
热议问题