Replies: 1 comment 3 replies
-
Hi @notcome,
This is not true. SwiftUI views are
Well, it is correct that mutations to state notify the registrar, but this happens in the
Yes, because reducers are main actor bound, they must completely finish before the UI has a change to render.
Can you first describe exactly what problem you are witnessing? It is not clear right now. Also it would help a great deal if you could share a minimal project that demonstrates the problem you are seeing. |
Beta Was this translation helpful? Give feedback.
-
I have a complex feature coordinating transitions among two pages. Attached at the end is a mutating method defined on State. Here is the setup:
IdentifiedArray
ofPageFeature.State
, namely the member.pages
.StoreOf<PageFeature>
.Now, I understand the code below is not optimal. For instance, I can skip initializing them to a default value then merging them -- all I need to do is to assign the final value. But, this not-optimal code seems to reveal a nasty issue:
If we update multiple children states in a reducer, SwiftUI might update views during the middle of the reducer.
In my case, when I initialize the transition values of my tab bar, SwiftUI has 50% chance to decide to update the other two pages, the corresponding states of which have already been touched. There is an unfortunately problem though:
A child store's state is not up-to-date until the root store has finished an action.
I am not sure if this is a bug or intended -- I guess it is the later. But this seems to give us a problem on semantics: we are notifying the systems that some members of our child stores have been changed, but the values are not there. If we have a guarantee that SwiftUI will not process these changes immediately, but to the end of the current loop, we will be fine. However, that does not seem to be the case, and SwiftUI's behavior isn't really deterministic here, according to my observation.
I could improve the code as I mentioned above, reducing writes to child stores. But I am not confident that this will solve the issue in all situations. I do find a workaround though, that is wrapping this store action inside a SwiftUI transaction -- it seems that SwiftUI transaction not only carries animation information, but is also a, as its name suggests, transaction.
Questions:
Beta Was this translation helpful? Give feedback.
All reactions