SwiftData Data Deleting Unwantedly

Hi,

I have two SwiftData models that are not working as intended. I'm able to add data to the models, but sometimes after refreshing the app data is missing. Any guidance would be greatly appreciated.

Hi,

I'm just following up to see if anyone has any thoughts on this.

The first thing I'd check is if the data is successfully persisted or not. You can do so by re-launching your app after the data is supposed to be saved and see if the data is still there.

By default, SwiftData autosave is only enabled for mainContext. When you work with a model context created using init(_ container: ModelContainer), autosave is disabled, and so you need to explicitly call save() to persist the data.

If you do explicitly save the data, and your issue is still there, please provide a runnable code snippet that reproduces the issue. I'd take a closer look.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

SwiftData Data Deleting Unwantedly
 
 
Q