Delayed save when tapping "back" button using a SwiftUI DocumentGroup

Hello all,

I've made an app that is using a SwiftUI DocumentGroup. It is almost done but one of the last bugs is when tapping 'back' off of the document it takes a while to save, about 15 seconds consistently. When creating a new app in SwiftUI that uses DocumentGroup via Xcode (the one that just has a text editor) it saves instantly on tapping the 'back' button. I've tested both my app and the initial app that Xcode gives you saving about the same amount of data, ~500 KB and the difference is still there.

Any ideas what could be going on here?

Thanks in advance.

Replies

So I've narrowed it down to an odd issue with a NavigationLink.

Using Xcode 12.5 when creating a new project (named Test3Document, please excuse the name) that is a document based app the ContentView.swift file created looks like this:

struct ContentView: View {

    @Binding var document: Test3Document

    var body: some View {
        TextEditor(text: $document.text)
    }
}

Everything works fine when editing then tapping the back button. It saves instantly and you can see this by opening the document right up again. It is immediately reflected.

When modifying it slightly like so:

struct ContentView: View {

    @Binding var document: Test3Document

    var body: some View {
        NavigationLink(
            destination: TextEditor(text: $document.text),
            label: {
                Text(document.text)
            }
        )
    }
}

Tapping the label, then editing the text, hitting the back button to go back to the initial view reflects immediately. However tapping the back button to get to the DocumentGroup, then opening the same document does not immediately reflect the change. Waiting ~15 seconds does show the change though.

Very strange!

cannot delete my post sorry.

I’m using Xcode 12.4 and ran into this same issue. My document is a ReferenceFileDocument (don't know if that influences this bug). My app initially presented an editor screen via a sheet. Closing the sheet and then backing out to the document browser would trigger an immediate save. Then I switched the editor view to be displayed via a NavigationLink. Then, as you report, the save would only be triggered after 15 seconds. I have decided to switch back to using a sheet to present my editor.

Regarding the 15 seconds, that’s the normal delay that triggers a save after making an edit. Normally that’s OK while you are still editing a document. But yeah, the minute you close the document, it should trigger a save.