Sharing Environment dependencies with UIViewRepresentable views #549
Replies: 2 comments 2 replies
-
|
Maybe you can use the If it's not possible, and without touching TCA inner bits, I would steal it from the reducer and store it in the state, in an optional class wrapper. It shouldn't affect automatic Equatable conformance (you can rely on the nil/non-nil state of the optional to trigger the You should setup your representable so it can handle a nil environment to begin with, as you're not guaranteed to have one when the I would recommend the first approach with direct injection though, as you can bypass optionals and count on a non-nil environment at any time. The setup is also way simpler and doesn't have 10 reasons to go wrong. You're most likely instantiating a I think it will not break any TCA principles if you're only using the environment as an environment. |
Beta Was this translation helpful? Give feedback.
-
|
I updated the topic on the swift forum https://forums.swift.org/t/local-shared-environment/39465/3 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, we have a situation where we need to share my
Environmentdependency with aUIViewRepresentableview. Unfortunately, environments are only available to the reducers, and theStoredoes not seem to allow sharing them in any way.In a related discussion @mbrandonw suggested that (paraphrasing) "
UIViewRepresentableviews should be treated as vanilla SwiftUI views" (at least for simple use cases that permit it). Our case, however, is more complex.We use
Mapboxthat we wrap in aMapViewin combination with Composable Core Location("CCL")LocationManagerto drive location updates. The wrappedMGLMapViewhas alocationManagerproperty, which can be overridden to gain control of map location updates.The first problem is somehow proxying the CCL's
LocationManagercalls and running it's effects. To replace thelocationManagerone has to adopt theMGLLocationManager(link) protocol.The second problem, is actually sharing the
LocationManagerfrom theEnvironmentwith theUIViewRepresentable.From my search I only found one related issue on the TCA Swift forums by @maximkrouk, and from my understanding of his solution:
he is using some globally shared environment dependency that is injected twice, first in the reducer, and second time in the
UIViewRepresentableview.What are some ways that this could be achieved without violating the TCA architectural patterns? Is there a way to run ad-hoc effects without the use of a
Store? We're super-duper blocked by this at the moment, and would really appreciate any solutions or workarounds that the community can share 🙏Beta Was this translation helpful? Give feedback.
All reactions