Hey everyone, I wanted to consume flow that I am e...
# multiplatform
d
Hey everyone, I wanted to consume flow that I am emitting from the shared view model. I am initially emitting three states Loading, Success and Error. In the success State I am passing the flow, for reference
data class Success(val data: Flow<LocalAnimeEntity>) : AnimeLocalState
. I am using
SKIE
to consume the flow (attached image of snippet). But I am getting this error log
Copy code
error: generic struct 'ForEach' requires that 'SkieSwiftFlow<LocalAnimeEntity>' conform to 'RandomAccessCollection'
                        ForEach(successState.data, id: \.id) { anime in
                        ^
SwiftUI.ForEach:2:15: note: where 'Data' = 'SkieSwiftFlow<LocalAnimeEntity>'
public struct ForEach<Data, ID, Content> where Data : RandomAccessCollection, ID : Hashable {
              ^
I tried to implement the example solution provided on the example in the docs, but is difficult to implement as I am not getting data directly. Can someone help thank you color
k
A
Flow
instance becomes an
AsyncSequence
in Swift. They're logically similar, as is SwiftUI to Compose in many ways. I'm not a SwiftUI expert, but
ForEach
looks like it takes a collection and cannot perform any async operations, which makes sense (that would be rather difficult in a declarative state-based UI system). Presumably Compose would present similar challenges. You'd probably want to collect all of the values on the Kotlin side into a List and use that instead of returning the
Flow
and attempting to have the UI use that. If it needs to be a flow, you'll need to loop over that in an async function and update some kind of observable state in Swift, which will redraw as new data is collected from the
Flow
. Example here: https://github.com/touchlab/SKIEDemoSample/blob/main/ios/ios/ChatRoomDemo.swift#L49
d
Thanks for both the suggestions, let me try the second approach first, if not then the first one looks viable.
t
Hey @Debanshu Datta, as Kevin said,
ForEach
is a SwiftUI component that takes in an Array and runs the provided block for each item, thus creating a separate view for each item. We have a prototype for a component that's similar to
ForEach
but instead of an array, you give it an
AsyncSequence
and it runs the block for each item once it arrives. But it's not ready for release yet, so keep an eye out on our blog (https://touchlab.co/blog) where we'll post about it in the near future.
d
Hey @Tadeas Kriz can you help me with the scenario at hand, like I have three states Loading, Success and Error. IN the sucess state I am passing a data: Flow<LocalEntity>, how do I consume it. With the example that @kpgalligan provided. I have limited experience with the iOS side of things your expertise will surely help me out. Here is the github repo. For reference the is the Shared View Model and here the SwiftUI. thank you color 🙏
t
Hey @Debanshu Datta, take a look at my reply to your SKIE issue from yesterday: https://github.com/touchlab/SKIE/issues/50#issuecomment-1867762792 Let me know if that helps. But as I mentioned in the issue, I believe your main issue is using
Flow<Entity>
in
Success
. What are you trying to model with the flow?