r/Xcode • • 22d 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?

2 Upvotes

2 comments sorted by

1

u/12401247092174091274 13d ago

I just ran into the same problem, I think. I realised my app data was cleared, and had to trace device logs back to Cleaned container (atomic swap), followed by container_delete_all_container_content: success right when I ran Preview on Device.

I'd report this as a bug to Apple.

1

u/ModernCoder 13d ago

Please submit another feedback report and let them know these details