FileDocument.fileWrapper not actually called on background thread

It's called on the main thread. So if it takes any time (e.g. compressing some data), your UI will lock up. Worse, it locks up your UI when auto-save kicks in (i.e. not in response to a user action). Same for ReferenceFileDocument.fileWrapper

Documentation says background thread:

Ensure that types that conform to this protocol are thread-safe. In particular, SwiftUI calls the protocol’s methods on a background thread. Don’t use that thread to perform user interface updates. Use it only to serialize and deserialize the document data.

Stack trace:

* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 3.1
  * frame #0: 0x000000010e0cf634 AsyncSave`AsyncSaveDocument.fileWrapper(snapshot="Hello, world!\n\nTest auto save\n\n", configuration=$s7SwiftUI30FileDocumentWriteConfigurationVD @ 0x00007ff7b1e307d0, self=(text = "Hello, world!\n\nTest auto save\n\n")) at AsyncSaveDocument.swift:51:47
    frame #1: 0x000000010e0cf9ca AsyncSave`protocol witness for ReferenceFileDocument.fileWrapper(snapshot:configuration:) in conformance AsyncSaveDocument at <compiler-generated>:0
    frame #2: 0x00007ff84a271109 SwiftUI`___lldb_unnamed_symbol129907 + 146

Must be a bug eh?

With the newer concurrency stuff, I would think that your FileDocument would have to be Sendable. I tried making it Sendable in sheer desperation, but fileWrapper was still called on the main thread.

What platform is it?

@julia_brockovich iOS. Seems to do the right thing on macOS (I just created a test project)

@julia_brockovich FB13439586

FileDocument.fileWrapper not actually called on background thread
 
 
Q