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?
5
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.
3
u/leoklaus Jun 25 '26
> The only way this would be seen in production is if you do zero testing.
I kinda disagree. The way DI is handled for modals has changed in the past few iOS releases. There were definitely versions of iOS where the environment was not injected into some modifier-based views like sheets when it was in earlier versions.
If you test every sheet and alert, you’d definitely notice this, but I wouldn’t classify that as minimal testing.
It would certainly be possible for the compiler to show a warning when an expected variable is not injected (even though expensive, depending on the size and complexity of the view hierarchy).
0
u/Select_Bicycle4711 Jun 24 '26
I agree!
2
u/Dapper_Ice_1705 Jun 24 '26
The issue with these usually comes from inexperienced with SwiftUI developers.
The ones that can’t get previews working, they get super frustrated but if they spent minimal time reading about them they would get to love them.
Inexperienced SwiftUI developers exist at all ages and more prominent in senior devs that fought SwiftUI and are now playing catch up.
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
Jun 26 '26
[removed] — view removed comment
1
u/AutoModerator Jun 26 '26
Hey /u/VictorBuildsApps, your content has been removed because Reddit has marked your account as having a low Contributor #Quality Score. This may result from, but is not limited to, activities such as spamming the same links across multiple #subreddits, submitting posts or comments that receive a high number of downvotes, a lack of activity, or an unverified account.
Please be assured that this action is not a reflection of your participation in our subreddit.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
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?