-
Hi, In my application, I have a tab view with 3 screens, each one of them and the TabView having their own I'm noticing that every time I'm changing between tabs, say going from tab 1 to tab 3 and back to tab 1, these "onAppear" effects are canceled every time I leave their screen, i.e tab 1 -> tab 3 => reducer 1 cancels its currently running "onAppear" effect. Is there a way to start an effect in a reducer, for its entire lifetime? i.e start an effect in the reducer's I was thinking I could spawn a long-running effect for each tab, in the Tab View reducer (which doesn't trigger At the same time, I think the current behavior of reducers may actually be the correct one and in fact, these observations should be canceled once the user moves to a different tab and restarted when the user comes back. In that case, I'm not sure how I should handle a long-running effect that makes a network request based on a timer (i.e fetch a random recipe every 30 seconds and display it in the UI). In this case, every time the user switches back and forth between tabs, a random recipe is shown less than 30 seconds at a time. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This should not be the case if you are using Could you share a minimal demo project that shows the problem you are seeing? |
Beta Was this translation helpful? Give feedback.
Yes, you probably do not want to use
.task
.I personally would recommend creating your own kind of
onFirstAppear
view modifier that that acts likeonAppear
, but fires its closure only once. You would use some local@State
in the modifier to determine that condition. You can also find versions of it online if you do not want to make your own.I would not recommend storing that state in your feature. It just isn't that important for your feature's logic, and so is fine to let the view take care of it.