I wonder swiftdata query cannot be used within a class

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.

Answered by DTS Engineer in 788556022

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.

If you are going to make only a simple query, I recommend you to do it in MainView. If you are going to apply other operations to your data, it may make sense to use a View Model. To make a query in a View Model, you must first pass the modelContext into it. After passing it, you should use FetchDescriptor to query. You can search what you want to do on the internet by saying Query in ViewModel. I tried to explain briefly. If you have any questions, please feel free to ask. I would be very happy to help.

Accepted Answer

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.

I wonder swiftdata query cannot be used within a class
 
 
Q