I'm trying to preload an app with read-only data contained in a default.store
file located in the app's bundle.
import SwiftUI
import SwiftData
@main
struct AppDemo: App {
enum Schema: VersionedSchema {
static var versionIdentifier: SwiftData.Schema.Version = .init(1, 0, 0)
static var models: [any PersistentModel.Type] = [Item.self]
}
let container: ModelContainer
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(container)
}
init() {
do {
let url = Bundle.main.url(forResource: "default", withExtension: "store")!
let schema = SwiftData.Schema(versionedSchema: Schema.self)
let configuration = ModelConfiguration(url: url)
container = try ModelContainer(for: schema, configurations: configuration)
} catch {
debugPrint(error)
}
}
}
I've used the approach shown in this example.
For reasons I don't know, it works perfectly in the simulator (iOS 17.0). But the same code fails when run on a device (iOS 17.0, still).
Unresolved error loading container Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={sourceURL=file:///private/var/containers/Bundle/Application/[UUID]/AppDemo.app/default.store, reason=Cannot migrate store in-place: error during SQL execution : attempt to write a readonly database, destinationURL=file:///private/var/containers/Bundle/Application/[UUID]/AppDemo.app/default.store, NSUnderlyingError=0x281c7ae20 {Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={NSSQLiteErrorDomain=8, NSFilePath=/private/var/containers/Bundle/Application/[UUID]/AppDemo.app/default.store, NSUnderlyingException=error during SQL execution : attempt to write a readonly database, reason=error during SQL execution : attempt to write a readonly database}}}
CoreData: Attempt to add read-only file at path file:///private/var/containers/Bundle/Application/.../AppDemo.app/default.store read/write. Adding it read-only instead. This will be a hard error in the future; you must specify the NSReadOnlyPersistentStoreOption.
How to configure the ModelContainer
as read-only?
allowsSave: false
inModelConfiguration.init
.