There is nothing wrong with the singleton pattern except you have to be careful with concurrency: you should probably mark your singleton class as @MainActor. This pattern is useful if you want to trigger something in the views in the code that doesn't have access to the view hierarchy. If it's not the case then this pattern is not required, just use plain environment values.
I use this pattern for the global isLoggedIn flag to trigger re-rendering of the root view when it changes.
So this is also possible:
@MainActor
class Singleton: ObservableObject {
@Published var flag: Bool = true
static var shared = Singleton()
}
struct SomeView: View {
@StateObject var singleton = Singleton.shared
var body: some View {
Text("Hello, Singleton\(singleton.flag ? "!" : "?")")
.environmentObject(singleton)
}
}
struct SomeOtherView: View {
@EnvironmentObject var singleton: Singleton
var body: some View {
Text("Hello again, Singleton\(singleton.flag ? "!" : "?")")
}
}
// Somewhere else in the code outside of Views:
Singleton.shared.flag = false
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: