In SwiftUI, is it a good idea to create a view model (or observed object) in a View? For example, if I write a code like this:
struct AView: View {
...
public var body: some View {
...
ForEach(...) {
...
otherView
}
}
private var otherView: some View {
let model = OtherViewModel()
return OtherView().environmentObject(model)
}
}
I guess this will re-create OtherViewModel every time AView is refreshed, so maybe this is not a good approach after all?
I thought about creating it in the constructor of AView, but if otherView is created multiple times using ForEach like the example above, it's difficult to know ahead of time how many of these models I need to create.
Any suggestion is highly appreciated!
Thanks,