UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Is @EnvironmentObject the new singleton?

Forums > SwiftUI

As someone who has used dependency injection for a long time and is generally not a fan of singleton use outside of pretty specific scenarios, @EnvironmentObject feels like the new singleton to me. Does anyone else have trouble with it? How does @EnvironmentObject fit in with testing and common iOS design patterns like MVVM and MVP?

Of course I realize that there's no reason that I have to use it personally, but I generally try to error on the side of features that Apple is pushing. SwiftUI and Combine are obviously the future. Help me feel good about @EnvironmentObject...

4      

I'm getting the same feeling. @EnvironmentObject feels very "singleton-y" to me. However I think there are some important differences:

  • It's a Combine publisher, which means it's automatically updating in the background and we don't have to remember to update it. Anything subscribed to it will already be updated.
  • SwiftUI is meant to have a single source of truth. The View is a function of the state, so the view never tells the ViewModel what to do.

In theory, this approach should have all the benefits of a singleton without the negative side effects.

The one thing I'm not sure of is, how does @EnvironmentObject know which object to put into that variable. If I only pass in one object of that type through .environmentObject() then it shouldn't be a big deal. It just finds the variable with the matching type.

But what happens if I have two .environmentObject() 's and two @EnvironmentObjects 's? How does it know which goes to which? My guess is we're probably not supposed to have more than one.

4      

The one thing I'm not sure of is, how does @EnvironmentObject know which object to put into that variable. If I only pass in one object of that type through .environmentObject() then it shouldn't be a big deal. It just finds the variable with the matching type.

But what happens if I have two .environmentObject() 's and two @EnvironmentObjects 's? How does it know which goes to which? My guess is we're probably not supposed to have more than one.

The environment is essentially a dictionary whose keys are the types of the objects within it. So you can't add more than one object of the same type without overwriting the original one.

5      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.