NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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...

   

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.

   

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.

   

Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

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

Reply to this topic…

You need to create an account or log in to reply.

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.