In a document-based SwiftUI application, I’d like to persist each document to a separate Sqlite file using GRDB as a Sqlite wrapper. It’s straightforward to load Sqlite files in a document that implements the FileDocument
protocol by creating a DatabaseQueue
for the file to be loaded and using its .backup(to:)
method to copy to an in-memory DatabaseQueue
. How should I implement saving in the func fileWrapper(configuration: WriteConfiguration)
method? There doesn’t seem to be an obvious way to use the same .backup(to:)
approach.
I found an example application by Andre Yonadam that approaches this in the same way in a subclass of NSDocument:
override func write(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType, originalContentsURL absoluteOriginalContentsURL: URL?) throws {
let destination = try DatabaseQueue(path: url.path)
do {
try memoryDBQueue.backup(to: destination)
} catch {
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}
}
override func read(from url: URL, ofType typeName: String) throws {
let source = try DatabaseQueue(path: url.path)
do {
try source.backup(to: memoryDBQueue)
} catch {
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}
}