If the entire views access the same model in an app, I think the Singleton pattern is enough. Am I right?
For example, if MainView and ChildView access the same model(e.g. AppSetting) like below, I cannot find any reason to use EnvironmentObject instead of the Singleton pattern. Is there any problem if I use like this? If it is okay, then when I should use EnvironmentObject instead of the Singleton pattern?
class AppSetting: ObservableObject {
static let shared = AppSetting()
private init() {}
@Published var userName: String = "StackOverflow"
}
struct MainView: View {
@ObservedObject private var appSetting = AppSetting.shared
var body: some View {
Text(appSetting.userName)
}
}
struct ChildView: View {
@ObservedObject private var appSetting = AppSetting.shared
var body: some View {
Text(appSetting.userName)
}
}
Thanks in advance.