I have an infinite flow of items, coming from the ...
# compose-desktop
r
I have an infinite flow of items, coming from the
net
, trying to subscribe to it from
@Composable
, but it seems to cancel the subscription coroutine, so connection is lost. I've tried with
LaunchEffect(Unit)
but no differences:
Copy code
var itemsListState by remember { mutableStateOf<List<LootItemViewObject>>(emptyList()) }
    val scope = rememberCoroutineScope()
    scope.launch {
        suggestedItemsRepository
            .subscribeForItem(qrCode)
            .collect {
                itemsListState = it
            }
    }
I think, I've missed one vital concept of recomposing, can you please help? This code is placed in
@Composable
which renders
LazyColumn
of those items. (it is later somehing like
LazyColumn { itemsListState.forEach { item { ...} } }
d
Well, let's start by using launched effect. What you have now with scope launch will cause problems.
How do you know it's cancelled?
r
@Dominaezzz I've used
LaunchedEffect
, the result was the same. I assume coroutine is canceled, because Flow
collection
is canceled.
j
Why you can’t use collectAsState?
Probably you can just check any compose sample which is using room for saving data, which exposes infinite flows. But probably this infinite flow is just collected as state in the parent and passing the list to the lazy column
r
@Javier trying right now.
I'll try to start subscription in one coroutine scope and collect it from @Composable with another one.
z
Could you maybe post more code showing that relationship?
f
It's great idea to have a view model that feeds the composable an observable/collectable stream of data that you can collect/manipulate, every time you update the mutableState of your list the coroutine is cancelled because recomposition happens and that's because i think that Unit is instantiated every time in your launched effect