Views in ForEach
recomputed upon element addition
#2973
Replies: 3 comments 2 replies
-
Hi @bohdany-cricut, thanks for bringing this up! It is true this happens in TCA today, and we have plans to address it, but it's going to take time to get there. Luckily this only affects I'm going to convert this to a discussion since it can't be addressed right away, but do know that we are working towards a world where this will be possible. Please feel free to continue the conversation over there if you want. |
Beta Was this translation helpful? Give feedback.
-
Trying to understand this statement - isn't Are there actually other options? Can we easily use ForEach with all the automatically generated niceties (IdentifiedAction for example for child-parent communication) without it? ... |
Beta Was this translation helpful? Give feedback.
-
hi @mbrandonw may i know about updated information about this issue? maybe there is workaround or fix that we can use? |
Beta Was this translation helpful? Give feedback.
-
Description
TCA 1.9.2 (also tested on 1.7.3 and
main
).When elements are added to an
IdentifiedArray
that drivesForEach
, everyView
inside the loop gets recomputed. This behavior is present in both iOS 17+ and earlier versions (withWithPerceptionTracking
). I have also tried the old method - usingWithViewStore
,ForEachStore
, andState
not being@ObservableState
, but it doesn't seem to make any difference.In vanilla SwiftUI, this does not happen in iOS 17+ with the
@Observable
macro. Only views corresponding to added elements get recomputed. However, it does happen in vanilla SwiftUI on iOS <17 with theObservableObject
protocol and@Published
.Please refer to the videos below::
Vanilla SwiftUI with
@Observable
macro:https://github.com/pointfreeco/swift-composable-architecture/assets/118171669/18370627-708f-4991-8a54-aefd3a846a60
TCA:
https://github.com/pointfreeco/swift-composable-architecture/assets/118171669/4e7bf5b9-aa85-4614-b1dd-9403adcfce1c
Sidenote:
I am using TCA in a large application with lists containing dozens of heavy views, and adding elements to the driving array while scrolling causes hangs because each view gets recomputed.
Checklist
main
branch of this package.Expected behavior
Views inside ForEach don't get recomputed, as in vanilla SwiftUI.
Ideally on iOS <17 as well.
Actual behavior
Every view that is placed in a
ForEach
gets recomputed when elements are added to the corresponding array.Steps to reproduce
Here is a sample project where this issue reproduces.
https://github.com/bohdany-cricut/TCA_Perception
TCA_PerceptionApp.swift
The Composable Architecture version information
1.9.2
Destination operating system
iOS 17.2
,iOS 15.0
Xcode version information
Version 15.3 (15E204a)
Swift Compiler version information
Beta Was this translation helpful? Give feedback.
All reactions