DocumentGroup crashes Playgrounds App Preview

steps to reproduce:

  1. create a new iPad Playgrounds project
  2. implement the minimum viable app using DocumentGroup
import SwiftUI
import UniformTypeIdentifiers

struct TextDocument: FileDocument {
    var text: String
    
    init(text: String = "") {
        self.text = text
    }
    
    static var readableContentTypes: [UTType] { [.plainText] }
    
    init(configuration: ReadConfiguration) throws {
        guard let data = configuration.file.regularFileContents,
              let string = String(data: data, encoding: .utf8)
        else {
            throw CocoaError(.fileReadCorruptFile)
        }
        text = string
    }
    
    func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
        let data = text.data(using: .utf8)!
        return .init(regularFileWithContents: data)
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        DocumentGroup(newDocument: TextDocument()) { file in
            TextEditor(text: file.$document.text)
        }
    }
}

I can compile and run the app, and use it to open and edit or create new plain text documents, but the App Preview crashes, saying "The requested index was outside the bounds of the array." and there's a red error badge on @main

I assume this is a bug, and I've submitted a feedback (FB12194610) but I'm looking for ways to work around it in the meantime. There are two annoyances

  1. the red error badge obfuscates when i really do have errors in my code
  2. I don't have previews

If I comment out the DocumentGroup code and insert a WindowGroup I can make the preview successfully load long enough that I can pause it. This fixes the error badge problem, but pausing the App Preview pauses all the view previews as well. And every time I run the project it restarts the previews. And I have to run the project pretty often because I can't use the previews!

So I'm hoping there's a better workaround, either by more permanently disabling App Preview, or with something like the App Preview equivalent of PreviewProvider to give me some control over how the App Preview renders.

Answered by grraham in 754252022

If anyone else with the same problem ever stumbles upon this post, my workaround is just to work with the WindowGroup in the Scene so I can use previews and get error warnings, and then comment it out when i want to actually build and run the app

@main
struct MyApp: App {
    @State var document: TextDocument = TextDocument()
    var body: some Scene {
        WindowGroup {
            NavigationStack {
                ContentView(document: $document)
            }
        }
        DocumentGroup(newDocument: TextDocument()) { file in
            ContentView(document: file.$document)
        }
    }
}
Accepted Answer

If anyone else with the same problem ever stumbles upon this post, my workaround is just to work with the WindowGroup in the Scene so I can use previews and get error warnings, and then comment it out when i want to actually build and run the app

@main
struct MyApp: App {
    @State var document: TextDocument = TextDocument()
    var body: some Scene {
        WindowGroup {
            NavigationStack {
                ContentView(document: $document)
            }
        }
        DocumentGroup(newDocument: TextDocument()) { file in
            ContentView(document: file.$document)
        }
    }
}
DocumentGroup crashes Playgrounds App Preview
 
 
Q