Swift Data for one off data storage

What is the best way to use swift data to store one off data models? For example an application state that you want to be persisted across application launches.

The only examples I've seen use arrays of items which wouldn't work for having just one application state.

Is it possible to query just one item?

Replies

Better to use @AppStorage for that

I have the same question. It looks like using SwiftData could simplify the storage and retrieval of structured data. For example,

struct Gamestats { var played: Int var numWins: Int var currStreak: Int var maxStreak: Int var guessDist: [Int] var winPercent: Int? }

Since this is aggregate data I would want to store just one copy of this struct.

Does anyone have an example where this is being done? All the examples I've seen involve storing a collection of data.

Thanks

The WWDC demo app Backyard Bird keeps a single entity for their DataGeneration type. They use it to populate the app with data and if an entity e it’s they don’t repopulate the database.

Add a Comment