I have a textfield and a checkbox, backed by core data. Changes to the checkbox should be kept out of any undo/redo operations.
The recommend approach (found on stack overflow) is the following snippet.
@IBAction func stateDidChange(sender: NSButton?)
{
//disable undo manager
context.processPendingChanges()
context.undoManager?.disableUndoRegistration()
//set value
let value = Bool(sender!.state == NSOnState)
<some NSManagedObject>.flag = value
//enable undo manager
context.processPendingChanges()
context.undoManager?.enableUndoRegistration()
}
But this is not working. When the user
- edits the textfield,
- updates the checkbox,
- and continues editing the textfield,
then changes to the checkbox are included in the undo action.
I also tried
NSNotificationCenter.defaultCenter().postNotificationName(NSUndoManagerCheckpointNotification, object: self.undoManager)
self.undoManager?.disableUndoRegistration()
//do work
NSNotificationCenter.defaultCenter().postNotificationName(NSUndoManagerCheckpointNotification, object: self.undoManager)
self.undoManager?.enableUndoRegistration()
I even tried it in the NSManagedObject subclass
var flag : Bool {
get {
self.willAccessValueForKey("flag")
let text = self.primitiveValueForKey("flag") as! Bool
self.didAccessValueForKey("flag")
return text
}
set {
let context = self.managedObjectContext!
context.processPendingChanges()
context.undoManager?.disableUndoRegistration()
self.willChangeValueForKey("flag")
self.setPrimitiveValue(newValue, forKey: "flag")
self.didChangeValueForKey("flag")
context.processPendingChanges()
context.undoManager?.enableUndoRegistration()
}
}