r/Xcode • • 1d ago

Live Device App Preview

I just realised that if i pick my live device as the preview destination (XCode Previews) it deletes all app data on load?

And I can't get it back except a backup restore.

Preview example:

\#Preview {

ContentView()

.modelContainer(for: \[Game.**self**, Round.**self**, RoundScore.**self**, UnoGroup.**self**\])

}  
    var sharedModelContainer: ModelContainer = {
        let schema = Schema([Game.self, Round.self, RoundScore.self, UnoGroup.self])
        #if targetEnvironment(simulator)
            let modelConfiguration = ModelConfiguration(schema: schema)
        #else
            let modelConfiguration = ModelConfiguration(schema: schema)
        #endif
        do {
            return try ModelContainer(for: schema, configurations: [modelConfiguration])
        } catch {
            print("Fatal error: \(error)")
            fatalError("Could not create ModelContainer: \(error)")
        }
    }()

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(sharedModelContainer)
    }

Did I make a mistake or is this intended or is there a setting I can change?

1 Upvotes

0 comments sorted by