I have built a SwiftUI FileDocument app like this.
I'm a bit confused how to use the $document Binding.
I know with ObservableObject I can do:
How can I react to changes on $document in my Non-GUI code?
Use case: I'm building an app, where for some changes in the document, there's a command sent over the network. This command should also be sent, if I use e.g. undo. That's why I don't want so send these updates from within my View but as a reaction to a change in $document.
Code Block struct MyApp: App { var body: some Scene { DocumentGroup(newDocument: MyFileDocument()) { file in DocumentView(document: file.$document) } } }
I'm a bit confused how to use the $document Binding.
I know with ObservableObject I can do:
Code Block document.objectWillChange .sink { _ in print("Document changed") }
How can I react to changes on $document in my Non-GUI code?
Use case: I'm building an app, where for some changes in the document, there's a command sent over the network. This command should also be sent, if I use e.g. undo. That's why I don't want so send these updates from within my View but as a reaction to a change in $document.
Sorry, I should have been more specific. I'm using this on macOS which will automatically implement undo/redo functionality for the document. I was assuming this was done on iOS as well. So thanks for this answer:didSet gets called for every update from the GUI, but when I undo the change (CMD + Z or from the menu) it does not get called.
I don't know how exactly document undo/redo functionality is implemented, but I would assume that there's an internal copy of the complete document saved for each undo/redo step (which might be actually not as inefficient through use of copy-on-write semantics?) That means the complete document gets switched out and didSet doesn't get called. Any ideas how I could react to changes on undo/redo?
I don't know how exactly document undo/redo functionality is implemented, but I would assume that there's an internal copy of the complete document saved for each undo/redo step (which might be actually not as inefficient through use of copy-on-write semantics?) That means the complete document gets switched out and didSet doesn't get called. Any ideas how I could react to changes on undo/redo?