Using @Shared state with BindableAction #3865
Replies: 2 comments 4 replies
-
UPD:I just implemented this wrapper by copying @dynamicMemberLookup
@propertyWrapper
public struct SharedState<Value> {
let underlying: Shared<Value>
init(_ underlying: Shared<Value>) {
self.underlying = underlying
}
// Reimplement all inits/properties/methods using `underlying` implementation
}The only downside I found (aside the approach being potentially unsafe in some scenarios) is that it's not included in However batch-replacing occurrences of Note I'm using swift-sharing-extensions for my keys, so in case someone gets the same issue and wants the same solution, your replacement format might be different |
Beta Was this translation helpful? Give feedback.
-
|
@maximkrouk This is due to an availability hole that was plugged in Swift 6.3 that such code was exploiting. It affected the Case Studies app and we PRed a fix here. var body: some View {
@Binding(store.$greeting) var greeting
@Binding(store.$name) var name
TextField("Greeting", text: $greeting)
TextField("Name", text: $name)
}While a little more verbose, it's honest about what's going on. We also think you should never have been able to pattern match on In TCA2 we hope to have the ergonomics much better here, where bindings are built into the system 😄 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Both
@SharedandBindingActionare powerful tools in TCA, and they used to work fine together. However, today I reopened one of my projects that had been on hold for about 3 months and was surprised to see a bunch of compilation errors related to this interaction.Here is a previous example that would compile some time ago, which is a bit confusing (because
Shared.valuesetter has been marked@unavailablefor quite a while 🌚)The following workaround fixes the compilation errors:
Questions about this approach
Could you please elaborate on potential problems with this solution?
From my current understanding:
BindableActionconcerns:case .binding(\.<#KeyPath#>):TextFieldwill triggercase .binding(\.<#KeyPath#>):@Sharedconcerns:AspectRatioPickersetsenum CaptureAspectRatiovalue to@Sharedstate, when capture button is pressedCameraFeaturereads this value (and a few other ones) and passes it as an argument toCameraClient)Fundamentally this pattern is unsafe and probably shouldn't be a part of the
swift-sharinglibrary, but local use with simple values on the MainThread looks pretty safe to me and smth like thisHas much better ergonomics than this:
Maybe it's with adding
@SharedStatefor TCA only or@UISharedwith availablevaluesetter? 🤔Beta Was this translation helpful? Give feedback.
All reactions