Hello,
After doing some more research and testing, I would like to share my findings here in the hope that it might help some of you.
Here are my findings:
- it is possible to programmatically delete the contents of the database:
modelContext.container.deleteAllData()
Then close and restart the app and no more content will be available.
- the path to the SwiftData SQLlite database
The following command outputs the path to the Swift DB:
let theURL = NSPersistentContainer.defaultDirectoryURL()
print("**** URL: \(theURL) ****")
What I have not yet understood is that if I initialise SwiftData as described by Apple and start my app, add items, then the above-mentioned path is still without content. Nevertheless, the app works without any problems and the data is persistent.
However, it is possible to define the path to the database in the code when initialising the ModelContainer.
var sharedModelContainer: ModelContainer = {
let schema = Schema([
Script.self,
])
let baseURL = NSPersistentContainer.defaultDirectoryURL()
let storeURL = baseURL.appendingPathComponent("ralfb-SyncDataV2.sqlite")
let modelConfiguration = ModelConfiguration(schema: schema, url: storeURL)
do {
return try ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
The path is defined via baseURL and storeURL, and when I then start the app I find the following files under the path (baseURL):
ralfb-SyncDataV2.sqlite
ralfb-SyncDataV2.sqlite-shm
ralfb-SyncDataV2.sqlite-wal
If I delete this data and the DerivedData, then all app data, including the persistent data, is deleted.
Regards,
Ralf