Post

Replies

Boosts

Views

Activity

Reply to ObservableObject as a singleton vs EnvironmentObject
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:
Jan ’23
Reply to Video not working on 16.4 simulator
Same issue here after upgrading Xcode to 14.3. HLS and mp4 files aren't showing the video, only playing audio. Strangely in some situations it does play fine, but I couldn't figure out exactly how.
Replies
Boosts
Views
Activity
Apr ’23
Reply to ObservableObject as a singleton vs EnvironmentObject
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:
Replies
Boosts
Views
Activity
Jan ’23