darkmoon_uk
01/10/2025, 2:15 AM@State
. Is this about narrowing the lifecycle of collection to only when that View is in the composition?
• Example shows:
// Collecting a flow into a SwiftUI @State property using a Binding, which requires the property to be of the same type.
Text("Bound counter using Binding: \(boundCounter)")
.collect(flow: viewModel.counter, into: $counter) // <----- Typo?
...but I don't see $counter
defined - is this a typo and it should be $boundCounter
?kpgalligan
01/10/2025, 2:22 AM$counter
, does indeed look like a typo to me.Tadeas Kriz
01/10/2025, 3:11 AM$counter
is a typo, should either be $boundCounter
or the boundCounter
property should be called counter
. Thanks for bringing that up.
As for the modifier, there are two ways to collect flows into SwiftUI, the Observing
view and the .collect
modifier. They ensure that the flow is being collected while the view is visible. If you need to collect a Flow outside of a SwiftUI view, for example in a ViewModel, you can use regular for await
loop in Swift (https://www.hackingwithswift.com/quick-start/concurrency/how-to-loop-over-an-asyncsequence-using-for-await).