SwiftUI: Why use ReferenceFileDocument vs FileDocument?

I'm building a document-based app in SwiftUI.

I see you can have a document that is a value type using FileDocument or a document that is a reference type using ReferenceFileDocument.

There is no discussion in the documentation or in the WWDC session as to when you should use one instead of the other. From the WWDC session, it seems that FileDocument is the preferred approach.

What are the use cases / conditions when it would make more sense to use a ReferenceFileDocument?

What are the drawbacks of FileDocument that ReferenceFileDocument addresses?

Thank you for any insights on this.
Post not yet marked as solved Up vote post of James Dempsey Down vote post of James Dempsey
1.2k views

Replies

Can we please get an answer from an Apple engineer on this?

I am also struggling to understand how to use ReferenceFileDocument correctly. An example of proper usage would be greatly appreciated!
After some trial and error I think I have created a working example of using ReferenceFileDocument. You can find the code here. I hope this helps someone else!
Hi,

has anybody figured out in the meantime the why's and when's (James original questions)?

I am currently trying to develop a macOS app with SwiftUI's new app lifecycle and a document package as persistence and am really struggling to find out what the best approach here is...

Thanks!
ReferenceFileDocument is for auto-saving documents in the background. It uses UndoManager to know when to save a document.

See this thread..

And here is a working example.