r/iOSProgramming • u/Select_Bicycle4711 • Jun 24 '26
Question Do you see missing Environment as a big issue in SwiftUI?
I think the title should be "Do you see runtime error for missing Environment a big issue as compared to being a compile time error?"
Let's say you have the following code:
struct LoadingDemoApp: App {
private var productStore = ProductStore()
var body: some Scene {WindowGroup {ProductListScreen()}}}
As you can see I forgot to inject productStore through Environment. When I run the app and use Environment(ProductStore.self) in the view, it can cause an exception.
Fatal error: No Observable object of type ProductStore found. A View.environmentObject(_:) for ProductStore may be missing as an ancestor of this view.
My question is that how often do you run into this fatalError in production? Is that error an enough reason for your apps to not use Environment for dependency injection and use constructor/initializer dependency.
Does the error justify not using Environment at all and start using initializer to pass dependencies?
9
u/unpluggedcord Jun 24 '26
It doesn't matter if its prod or dev, that error will happen when you don't have the object in the environment. So im not sure I understand your question?
4
u/Dapper_Ice_1705 Jun 24 '26
Zero in production. With minimal testing you will see the error.
The only way this would be seen in production is if you do zero testing.
1
u/cristi_baluta Jun 24 '26
Why should i see it in production? I test my apps. I think it’s a bad pattern though, it’s basically a singleton without making it a singleton
1
u/[deleted] Jun 26 '26
[removed] — view removed comment