I’ve MainView. I’ve ContainerView that takes 3 totally different view fashions on this view.
I am making service queries within the view fashions of subviews (Container View). These queries are repeated when the father or mother view modifications. I do not need it to be repeated.
ContainerViewModel is consistently inited from scratch each time “showHeader” and tab modifications. How am i able to stop this?
(My venture helps iOS 13, I can’t use StateObject.)
`
struct MainView: View {
// MARK: - Properties
@EnvironmentObject var viewModel: MainViewModel
@State personal var selectedCategory: CategoryType = .first
// MARK: - Physique
var physique: some View {
HeaderView() { class in
selectedCategory = class
}
TabView(choice: $selectedCategory) {
ContainerView(viewModel: ContainerViewModel(selectedCategory: .first))
.tag(CategoryType.first)
ContainerView(viewModel: ContainerViewModel(selectedCategory: .second))
.tag(CategoryType.second)
ContainerView(viewModel: ContainerViewModel(selectedCategory: .third))
.tag(CategoryType.third)
}
}
}
`
`
struct ContainerView: View {
// MARK: - Properties
@EnvironmentObject var viewModel: ContainerViewModel
@State personal var showHeader = true
// MARK: - Physique
var physique: some View {
if showHeader {
HeaderView() { class in
selectedCategory = class
}
}
TabView(choice: $selectedCategory) {
ContainerView(showHeader: $showHeader)
.environmentObject(ContainerViewModel(selectedCategory: .first))
ContainerView(showHeader: $showHeader)
.environmentObject(ContainerViewModel(selectedCategory: .second))
ContainerView(showHeader: $showHeader)
.environmentObject(ContainerViewModel(selectedCategory: .third))
}
}
}
class ContainerViewModel: ObservableObject {
@Printed var selectedCategory: CategoryType
init(selectedCategory: CategoryType) {
self.selectedCategory = selectedCategory
// Community requests transactions...
}
}
`