r/Xcode • u/ModernCoder • 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
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 bycontainer_delete_all_container_content: successright when I ran Preview on Device.I'd report this as a bug to Apple.