View flashes when setting new PDFDocument in PDFView

I have a SwiftUI View containing a PDFView from PDFKit (via NSViewRepresentable). When setting a new PDFDocument the view flashes briefly with the background color before displaying the new document.

Curiously the flashing is vastly reduced (but not eliminated) by calling layoutDocumentView() which should already be called from setDocument according to its documentation.

struct SheetView: NSViewRepresentable {
    var pdf: PDFDocument?
 
    func makeNSView(context: Context) -> PDFView {
            
        let pdfView = PDFView()
        pdfView.displaysPageBreaks = false
        pdfView.displayMode = .singlePage
        pdfView.pageShadowsEnabled = false
    
        pdfView.autoScales = true
            
        return pdfView
    }
 
    func updateNSView(_ pdfView: PDFView, context: Context) {
        if pdf != pdfView.document {
            pdfView.document = pdf
            pdfView.layoutDocumentView() // reduces flashing but does not eliminate it
        }
    }
}

I don't know whether it matters, but the view is updated via a .task() modifier like follows:

struct ContentView: View {
    var model: Model
    @State var pdf: PDFDocument?

    var body: some View {
        HStack {
            SheetView(pdf: pdf)
            ModelView(model: $model)
                .task(id: model) {
                    pdf = await createPdf(from: model)
                }
        }
    }
}

func createPdf(from model: Model) async -> PDFDocument? { 
    ... 
}

where ModelView contains some controls for changing the model which triggers the task() modifier which creates a new PDFDocument and assigns it to the @State var pdf.

All that works nicely with the only caveat that the update of the PDFView within the SheetView flashes.

Thanks for any help!

-Thorsten

View flashes when setting new PDFDocument in PDFView
 
 
Q