Master Data + NSTextView + Undo Manager

I am trying to do a non-trivial job for Core Data with NSTextView support. I want to store not the entire NSTextStorage in my Core Data model, but instead break it up into paragraphs and store these paragraphs each in a separate object.

I actually got everything to work. But I can't seem to handle the undo / redo support. If I save every change in the TextDidChange notification or something, I basically lose NSTextView support because Core Data starts to store every single character that the text store entered. Therefore, the undo manager logs these actions individually. And when I undo, I only undo character by character, and that's not what I want. If I don't save the text I entered to Core Data, the text undo control stops.

How do I know when the undo manager starts and closes the group of activities, so I could only store the changes when the group is closed?

I tried to observe NSUndoManagerDidCloseUndoGroupNotification NSUndoManager, but it didn't help because I get this notification during every character input and still get annoying behavior from character-by-character behavior.

Is it possible to get a workaround?

UPDATE: I was able to achieve my goal by disableUndoRegistration () while I am updating the Core Data object. But I am wondering if there is another solution.

+3


source to share


1 answer


It seems to work. In the textStorageDidProcessEditing delegate method, I am calculating the changes made to the NSTextStorage. And find the correct paragraph elements in my Core Data model. Then I update the model, but before I start, I will disable unregistering the unregistered control of the ObjectContext and enable it when I finish updating the model. All this I do is the textStorageDidProcessEditing delegate method. Have experienced this all night. No errors found. The NSTextView undo manager is working as expected and my model is updated to reflect these text changes. Finally, the Core Data model does not have a big vehicle NSAttributtedString, but an NSOrderedSet of entities, each representing a unique NSTextView paragraph (thanks to NSUUID).



+1


source







All Articles