admin管理员组文章数量:1287607
I have a parent view and a child view. The child view requires a view model, and the view model requires an environment variable. Is there a way to avoid reinstantiating my view model every time SwiftUI renders the body? Is my overall approach to injecting the view model fine?
struct SettingsView: View {
@Environment(\.userSessionRepository) var sessionManager
private var accountViewModel: AccountSectionViewModel?
var body: some View {
NavigationStack {
Form {
AccountSectionView(viewModel: createAccountViewModel())
AppearanceSectionView()
HelpAndSupportSectionView()
}
}
}
private func createAccountViewModel() -> AccountSectionViewModel {
return AccountSectionViewModel(
coreDataClearing: CoreDataClearingManager(),
sessionManager: sessionManager
)
}
}
I have a parent view and a child view. The child view requires a view model, and the view model requires an environment variable. Is there a way to avoid reinstantiating my view model every time SwiftUI renders the body? Is my overall approach to injecting the view model fine?
struct SettingsView: View {
@Environment(\.userSessionRepository) var sessionManager
private var accountViewModel: AccountSectionViewModel?
var body: some View {
NavigationStack {
Form {
AccountSectionView(viewModel: createAccountViewModel())
AppearanceSectionView()
HelpAndSupportSectionView()
}
}
}
private func createAccountViewModel() -> AccountSectionViewModel {
return AccountSectionViewModel(
coreDataClearing: CoreDataClearingManager(),
sessionManager: sessionManager
)
}
}
Share
Improve this question
edited Feb 23 at 15:04
Joakim Danielson
52.1k5 gold badges33 silver badges71 bronze badges
asked Feb 23 at 8:32
user_personuser_person
1251 gold badge2 silver badges7 bronze badges
5
|
1 Answer
Reset to default 1You could try this approach using .onAppear
, when you have
@StateObject
or if you pass a
@ObservedObject var accountViewModel: AccountSectionViewModel
from the parent View.
See also
Monitoring data, or for iOS17+
Managing model data in your app using @Observable
.
Don't try to use init()
because the sessionManager
will
not be available at that time.
struct SettingsView: View {
@Environment(\.userSessionRepository) var sessionManager
// @StateObject private var accountViewModel = AccountSectionViewModel()
@ObservedObject var accountViewModel: AccountSectionViewModel
var body: some View {
NavigationStack {
Form {
AccountSectionView(viewModel: accountViewModel) // <-- here
AppearanceSectionView()
HelpAndSupportSectionView()
}
}
.onAppear {
// add the required elements
accountViewModel.coreDataClearing = CoreDataClearingManager()
accountViewModel.sessionManager = sessionManager
}
}
}
本文标签: swiftView model is instantiated twiceStack Overflow
版权声明:本文标题:swift - View model is instantiated twice - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741317591a2372010.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
AccountSectionView
? If you do it in a@StateObject
initialiser, it will only be created once throughout the lifetime ofAccountSectionView
. (assumingAccountSectionViewModel
is anObservableObject
) – Sweeper Commented Feb 23 at 8:40AccountSectionView(account: account)
however you are only supposed to pass it what is needed, e.g.MainSectionView(title: account.nam)
– malhal Commented Feb 23 at 14:22