Hello I'm trying the new Observation in SwiftUI, I created 2 classes using the new @Observable wrapper like this:
@Observable
class CategoriesViewModel {
var categories: [Category] = []
}
@Observable
class NotesViewModel {
var notes: [Note] = []
}
and I'm using them in the views like this:
@Environment(CategoriesViewModel.self) var categoriesViewModel
The question is: what if I want to use an observable inside another observable ? for example, I want to use the categories inside the NotesViewModel, I tried the @State instead of @Enviroment but it's not a global across all classes, so I want to use it as an environment variable like what I did in the views, I tried to use it inside the Observable class like this but I got an error below:
@Observable
class NotesViewModel {
var notes: [Note] = []
@ObservationIgnored
@Environment(CategoriesViewModel.self) var categoriesViewModel: CategoriesViewModel
func foo(){
/// Thread 1: Fatal error: No Observable object of type CategoriesViewModel found. A View.environmentObject(_:) for CategoriesViewModel may be missing as an ancestor of this view.
print(categoriesViewModel.categories.count)
}
}