Did you get any resolution on this?
In the same boat here and wondering what the best-practice / solution you ended up going with.
Post
Replies
Boosts
Views
Activity
This still seems to be happening as of iOS 16.1.
What's the status of the radar bug tracking this issue?
How do you get this working with previews though?
Presumable with previews you may want to pass in dummy environment objects/services:
MyView().environment( \.database, database )
whereas in the runnable app you might want the hosting controller to pass along other services.
Specifying it in the view definition itself doesn't allow you to pass it in from the outside.
UPDATE: Ended up creating a wrapper view MyView_RUNTIME which has the runtime dependencies specified.
This way in the #preview block you can instantiate MyView with your dummy environment variables, while in the UIHostingController you can instantiate the real ones.
ie:
// use within UIHostingController (ie: super.init( coder: aDecoder, rootView: DebugView_RUNTIME() ))
struct MyView_RUNTIME : View {
var body: some View {
MyView().environment( \.database, ServiceLocator.database ) // NOTE: real DB registered during the bootstrapping of the runtime app
}
}
// use within previews
#Preview {
MyView().environment( \.database, MyDatabase( inMemory: true )) // in-memory, dummy database
}