-
Describe the bug A typical example of TCA usage advocates accessing state via
However, it results in no updates in Swift UI hierarchy in some "pretty innocent" cases of To Reproduce Please see https://github.com/grigorye/SwiftUIBodies. Expected behavior I would expect that Screenshots Please see https://github.com/grigorye/SwiftUIBodies. Environment
Additional context The problem lies in the fact that In the simplest case, a wrapper like below might be used to avoid the possibility of capturing the
Usage:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 14 replies
-
I didn't have the time to investigate much, but please note that manually installing an There's a subtlety in SwiftUI's invalidation process that I'm missing because I don't get why the A quick glance at the |
Beta Was this translation helpful? Give feedback.
-
This isn't a problem with TCA per se, and would be easily reproducible in a vanilla SwiftUI application too. In fact we have some bugs filed with Apple that demonstrate the problem (here and here). However, there is an easy change that fixes the problem, and it's probably the better way of constructing your views anyway. Although most SwiftUI views involve closures in some way, 99% of the time they are non-escaping closures. That means the closures aren't actually meant to be stored away and lazily evaluated later, but rather they just serve as a convenient syntax and are evaluated immediately. For example, the extension Section where Parent : View, Content : View, Footer : View {
public init(@ViewBuilder content: () -> Content, @ViewBuilder header: () -> Parent, @ViewBuilder footer: () -> Footer)
} This is how SwiftUI is meant to be used. So, if you change all of your views to no longer take escaping closures in the initializers, and instead eager evaluate the closures, you will see that everything works as you expect: struct FancyTextWrapper: View, Traceable {
let content: Text
init(@ViewBuilder content: () -> Text) {
self.content = content()
}
var body: some View {
content
}
} Hope it helps! Also, I'm going to convert this issue to a discussion since it does not seem to be a bug in the library. |
Beta Was this translation helpful? Give feedback.
-
Just in case, I could not reproduce anything meaningful with the second example. Left a comment in the gist. |
Beta Was this translation helpful? Give feedback.
This isn't a problem with TCA per se, and would be easily reproducible in a vanilla SwiftUI application too. In fact we have some bugs filed with Apple that demonstrate the problem (here and here).
However, there is an easy change that fixes the problem, and it's probably the better way of constructing your views anyway. Although most SwiftUI views involve closures in some way, 99% of the time they are non-escaping closures. That means the closures aren't actually meant to be stored away and lazily evaluated later, but rather they just serve as a convenient syntax and are evaluated immediately.
For example, the
Section
view takes view builder closures for parent, content and footer views,…