How do I properly access package FileWrapper's in UIDocument's

I'm trying to figure out how to do package FileWrappers w/ UIDocuments (or SwiftUI ReferenceFileDocuments) properly. I.e. so that they also sync reliably as iCloud documents.

In the load/init I get the NSFileWrapper and Apple's examples just store that (and also return or create that in the respective "save" operation).

If I subsequently access files in the wrapper (e.g. either reading or writing to them, say a SQLite DB), do I have to protect that using NSFileCoordinators myself? Probably?

Do I have to use NSFilePresenter manually to detect changes to the wrapper? In particular w/ ReferenceFileDocuments, which do not have a "load" method, just an init (are they recreated from scratch when a wrapper was modified by a different device?).

Also, the methods just return a FileWrapper, which is a class, how is that thread safe (e.g. for background snapshots) if I return the same one I got in the load? Could someone clarify the whole snapshotting? E.g. how would I properly snapshot a big package filewrapper containing lots of images or other files?

Replies

I have the same question. Did you find a solution for yourself?

I tried using a UIManagedDocument from a SwiftUI ReferenceFileDocument (which under the hood is using UIDocument)... but this FileWrapper based saving is a headache, so I'm using it only to create the initial document structure. (UIManagedDocument needs an URL to open, which we only have after the document has been read.)

I've also tried now to instantiate my own NSPersistentContainer within the my ReferenceFileDocument based class. But I see some "warnings" whenever a save is triggered:

2021-12-17 17:29:40.855047+0100 CDDocument[35156:1542568] [logging] BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode linked while in use: /Users/philipp/Library/Developer/CoreSimulator/Devices/9A35CF23-0961-46BE-9F29-41C974BD6E08/data/Containers/Shared/AppGroup/7170A731-86C9-4340-988B-BDD0FDB4CFAA/File Provider Storage/Untitled.example/StoreContent/persistentStore

2021-12-17 17:29:40.855105+0100 CDDocument[35156:1542568] [logging] invalidated open fd: 6 (0x10)

According to my understanding the SQLite files are moved away when the document is saved and replaced with those returned by the FileWrappers. So currently I'm returning the existing FileWrapper (which is the documents root directory)... nevertheless I get those errors.

I personally do not have experience with file coordination, so I'm wondering if there is really something I can do as I'm not really managing the files of my document myself.