import SwiftUI
import SwiftData
class DateManagerStore : ObservableObject {
@Query private var myData: [myData]
@Published var myDataToString = ""
func hopitalDataQuery() {
if let lastMyData = myData {
self.myDataToString = String(lastMyData.sorted(by: {$0.visitedDate > $1.visitedDate}).last)
}
}
}
struct MainView: View {
@EnvironmentObject var dateManagerStore : DateManagerStore
var body: some View {
VStack{
Text("\(dateManagerStore.myDataToString)")
}
.onAppear(perform: {
dateManagerStore.hopitalDataQuery()
})
}
}
I thought it would be good to manage SwiftData values used within multiple views in one place.
I wanted to use Query data in the DateManagerStore class declared as ObservableObject through onApper of the MainView.
However, when printing the myData variable within hopitalDataQuery() of the DateManagerStore class, empty data was output.
I tried to use @Query defined inside the DateManagerStore class in various ways, but none of the methods allowed me to put a value into the @Query variable 'myData'.
There is no error in Xcode itself, but no data is coming in. I can't find any related information anywhere, so I ask if it's officially not possible.
Yeah, @Query
relies on the model context (ModelContext
) in the SwiftUI environment, and so you can only use it with a SwiftUI view.
If you'd like to have your own data model provide a SwiftData result set, consider using FetchDescriptor
to fetch the data. If you need to get notified of the changes on the store to update the result set, consider observing the didSave notification.
Having said that, feel free to file a feedback report for the SwiftData team to consider making @Query
easier to use outside of a SwiftUI view.