Performance issue when using ForEachStore & TabView #2148
-
I'm currently rewriting the movie container into an app. And I ran into the problem that when changing the Console example:
But now:
Code:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @Alex0Klain, the problem is in the lines where you are constructing the view store, such as this: WithViewStore(
self.store,
observe: { $0 }
) { viewStore in …and this: self.viewStore = ViewStore(store) You are observing all of state in both cases, even though you only need a small amount of state. The Also, this bit of code is very problematic: struct MovieView: View {
init(
_ store: Store<MovieState, MovieActions>
) {
self.store = store
self.viewStore = ViewStore(store)
viewStore.send(.onLoad)
}
You should not be sending actions in a view's initializer. You shouldn't really be doing any heavy work whatsoever (this goes for vanilla SwiftUI too) since views can be constructed many times. A view struct being initialized does not represent the view "loading" in the normal sense. |
Beta Was this translation helpful? Give feedback.
Hi @Alex0Klain, the problem is in the lines where you are constructing the view store, such as this:
…and this:
You are observing all of state in both cases, even though you only need a small amount of state. The
PagerView
only needscurrentIndex
andMovieView
only needsid
. You should be using theobserve
argument to chisel away at the bare minimum of state that the view needs to do its job. There is an entire article devoted to this subject.Also, this bit of code is very problematic: