Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EnvironmentObject vs Singleton in SwiftUI?

Tags:

mvvm

swiftui

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.

like image 953
user2848557 Avatar asked Apr 22 '20 10:04

user2848557


People also ask

What is a singleton in Swiftui?

Swift version: 5.6. Paul Hudson @twostraws April 11th 2022. Singletons are objects that should only ever be created once, then shared everywhere they need to be used.

What is EnvironmentObject Swiftui?

An @EnvironmentObject is an object living in the current environment, available to read whenever needed. An environment object is defined at a higher-level view, and can any child view can access it if needed.

Why Singleton is used in Swift?

In Swift, Singleton is a design pattern that ensures a class can have only one object. Such a class is called singleton class. An initializer allows us to instantiate an object of a class. And, making the initializer of a class restricts the object creation of the class from outside of the class.

Can singleton class be inherited Swift?

A programmer cannot inherit the pure-singleton class. When you try to inherit any class in swift, you must call the constructor of the superclass.


2 Answers

You are correct there is no reason in this case to use an EnvironmentObject. Apple even encourages to make no excessive use of EnvironmentObjects.

Nevertheless an EnvironmentObject can be great too, if you use an object in many views, because then you don't have to pass it from View A to B, from B to C and so on.

Often you find yourself in a situation where even @State and @Binding will be enough to share and update data in a view and between two views.

like image 143
Kuhlemann Avatar answered Sep 28 '22 21:09

Kuhlemann


I think when your app supports multiple windows via Scene (example Document based apps) maybe the singleton is not a solution and the EnvironmentObject is better. Example you want to share selectedColor. When you use a singleton, the selectedColor will be same in entire app, in every scene (in every view in window). But if you want to use separated settings the EnvironmentObject is convenient.

like image 24
feca Avatar answered Sep 28 '22 19:09

feca