How to change SwiftData’s underlying storage filename
How to change SwiftData’s underlying storage filename 관련
Updated for Xcode 16
When you use modelContainer(for:)
you get a default configuration, which means SwiftData decides where your database is stored. That might be in your application support directory, but might also be in a shared location if you’re using app groups. However, if you want a custom location, you need to create a ModelConfiguration
instance by hand, then use that to create your container.
For example, if you wanted to write to an exact file in your documents directory rather than the default location of default.store, you would use code like this:
@main
struct RecipeBookApp: App {
var container: ModelContainer
init() {
do {
let storeURL = URL.documentsDirectory.appending(path: "database.sqlite")
let config = ModelConfiguration(url: storeURL)
container = try ModelContainer(for: Recipe.self, configurations: config)
} catch {
fatalError("Failed to configure SwiftData container.")
}
}
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(container)
}
}
This kind of custom location is helpful for when you’re working with an existing database, or perhaps if you want to read or write the database directly – something you can do if absolutely required, but should be done only with great caution.