Closed mister11 closed 3 years ago
Consider using mutableStateListOf()
.
Are you sure that you can start a coroutine like this ?
val coroutineScope = rememberCoroutineScope()
coroutineScope.launch {
generateSequence { simulateMoneyUpdates() }
.asFlow()
.onEach { delay(2000) }
.collect { payload ->
coinsState.value = payload
}
}
To me, it looks like you should use a LaunchedEffect(true) { ... }
, with your generateSequence { ... }.asFlow().collect { ... }
call inside to avoid incorrect behaviour on recomposition.
Consider using
mutableStateListOf()
.
Changing code to:
val coinsState = remember { mutableStateListOf<Coin>() }
// ...
generateSequence { simulateMoneyUpdates() }
.asFlow()
.onEach { delay(2000) }
.collect { payload ->
coinsState.clear()
coinsState.addAll(payload)
}
shows the same behavior.
Are you sure that you can start a coroutine like this ?
val coroutineScope = rememberCoroutineScope() coroutineScope.launch { generateSequence { simulateMoneyUpdates() } .asFlow() .onEach { delay(2000) } .collect { payload -> coinsState.value = payload } }
To me, it looks like you should use a
LaunchedEffect(true) { ... }
, with yourgenerateSequence { ... }.asFlow().collect { ... }
call inside to avoid incorrect behaviour on recomposition.
I honestly didn't know about it. I'm still new to Compose. But, changing to code to use LaunchedEffect
:
LaunchedEffect(true) {
generateSequence { simulateMoneyUpdates() }
.asFlow()
.onEach { delay(2000) }
.collect { payload ->
coinsState.clear()
coinsState.addAll(payload)
}
}
shows the same behavior.
@mister11 I run your code on 1.0.0-alpha1-rc1 and it worked fine (data refreshed without scrolling). Do you still experience this issue?
@akurasov I can confirm it works with alpha-2 (latest) release. Thank you for the fix and for letting me know :)
Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.
Environment info
Version: 0.3.1 OS: Arch Linux IDE: IntelliJ Java version: 11 Kotlin version: 1.4.30
Problem explanation
I have a somewhat strange issue when it comes to rendering updates when using
LazyColumn
and I'm not able to find any errors on my side so it might be something strange happening under-the-hood.The code sample is a bit longer, but simple - it has a hardcoded list of
Coin
objects that are set as items (indexed) of aLazyColumn
. Every 2 seconds, I simulate an update of that list (the original code was using RSocket, so this is for simplicity). When an update comes, there's no update of theLazyColumn
view until I start scrolling.Now, here's the strange part. I wanted to create a sample project for a bug report and I copied the whole thing to a new project and just replaced it with a smaller model called
User
. When I ran the app with it, everything worked, so I started experimenting.I took the original code that's using
Coin
and not updating the list and mapped each item toUser
leaving everything else exactly the same and this worked - the list is refreshed on every update. The code sample below is a version withCoin
that doesn't work. To make it work, uncomment line 78 that artificially maps it to theUser
model and change type fromCoin
toUser
in line 98.Code example