I would like to have a settings entity with SwiftData. I would like it to store my active entity that the user was working with when they last used the app, plus a few other settings that I would like to persist between uses.
Unfortunately if I use @Query var setting: Setting
, it doesn't work because it requires an array. But if I use it as an array with the intention to only use the first index @Query var setting: [Setting]
I have issues, as I will not be able to use setting as an ObservableObject
throughout my app (that I know).
I've tried @Binding
, but I'm afraid I just dont know enough about SwiftData, and I would appreciate any help.
I'm also pretty sure that if I query or bind the settings without attaching it to the modelContainer it isn't going to work anyways.
Surely there is an easier way to have my settings persist.
import SwiftUI
import SwiftData
@main
struct DataTestApp: App {
var sharedModelContainer: ModelContainer = {
let schema = Schema([
Item.self,
Setting.self,
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
do {
return try ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
@Binding var settings: Setting
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(sharedModelContainer)
.environmentObject(settings)
}
}
AppStorage
unsuitable? – Natka