How to properly add data files into a SwiftData ModelDocument file package

Hi all, I am working on a macOS/iOS sandboxed app with SwiftUI/SwiftData. The app saves its' data into a ModelDocument file. But I want to also save large binary data files into this file.

I create the DocumentGroup scene with:

    DocumentGroup(editing: Entry.self, contentType: .myDocument) {
        MainWindowView()
    }

In my MainWindowView, I use

@Environment(\.documentConfiguration) private var documentConfiguration

to get the URL to the user created ModelDocument file URL.

When I need to add large binary file into the ModelDocument file, I call a method in my model:

func saveReferencedData(_ data: Data, documentURL: URL?) throws {
    let logger = Logger(subsystem: "saveReferencedData", category: "Asset")
    
    if let documentURL {
        let referencedFileName = "\(entryIdentifier)_\(assetIdentifier).\(assetType)"
        let tempFileURL = documentURL.appending(components: referencedFileName)

        if documentURL.startAccessingSecurityScopedResource() {
            do {
                try data.write(to: tempFileURL, options: [])
            } catch {
                documentURL.stopAccessingSecurityScopedResource()
                throw AssetFileOperationError.unableToSaveReferenceFile
            }
            self.referencedFileLocation = referencedFileName
            
            logger.debug("Successfully saved image data to: \(referenceFileURL)")
        }
        documentURL.stopAccessingSecurityScopedResource()
    } else {
        logger.debug("ERROR! Unable to save referenced image file because document URL is nil.")
    }
}

When this method is called, the data file is saved, but immediately I gets this diablog box:

If I click on any of the options in the dialog box, the binary data file is removed.

I think this is a permission problem and I am not writing to the ModelDocument file correctly. But I can not find any information online to experiment more.

Does anyone have experience with a customized ModelDocument file with more data files written into it? Or if you have any insight or advice, it would be greatly appreciated.

Thank you so much for reading.

Answered by DTS Engineer in 795485022

When using DocumentGroup + SwiftData, DocumentGroup takes over the read and write of the document. There is no API for developers to overwrite the read or write process, and so there is no way for you to put an extra file to the document.

Given this situation, I can only suggest that you file a feedback report, with your concrete use case, for the SwiftUI team to consider.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

To further demonstrate the problem, I have uploaded a Xcode test project to GitHub. Link below:

Test project at GitHub

Accepted Answer

When using DocumentGroup + SwiftData, DocumentGroup takes over the read and write of the document. There is no API for developers to overwrite the read or write process, and so there is no way for you to put an extra file to the document.

Given this situation, I can only suggest that you file a feedback report, with your concrete use case, for the SwiftUI team to consider.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

How to properly add data files into a SwiftData ModelDocument file package
 
 
Q