Using SwiftData I have one relatively short list of @Model class objects that are referenced in several views. I thought it would be handy to query once and share via the environment but my Table in other views does not update when data changes as it does when I repeat the Query in each view. Is this a bad idea or perhaps I implemented it improperly?
@Observable
class AllAccounts: ObservableObject {
var accounts: [Account] = []
}
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
// an instance of the AllAccounts class, will save Query results to all.accounts
@StateObject var all = AllAccounts()
// one query for all accounts once in app, store in the environment`
@Query private var allAccounts: [Account]
.onAppear {
all.accounts = allAccounts
}
.environment(all)