How do I react to changes on `FileDocument`

I have built a SwiftUI FileDocument app like this.

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.
Answered by Yoko_Ono in 648241022
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?
Unsure if you meant the command for undo like shaking the phone or just text, but if you are just wanting to react to changes on the document via text, you could just add an observer to the text variable in the *Document.swift file.

Code Block language
var text: String {
  didSet {
print("Document changed")
}
}


Anytime the text changes in the document, the didSet observer will be called. I tried messing around with some custom Bindings, but it wasn't as straightforward/elegant.
Accepted Answer
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?
That is interesting @Yoko_Ono. I made the project for MacOS, and didSet does get called when I hit CMD + Z (on both undo and redo). Although, it seems very inconsistent what it undos. Either it will just do one work or everything that was done in the document.
Hmm, I think it only works on TextEditor, which is the default in a new document-based app. If you replace it with TextField, undo doesn't call didSet anymore. Also any other action (e.g. adding / removing and array item) in the document doesn't call a didSet on undo. This is quite strange...
How do I react to changes on `FileDocument`
 
 
Q