Ayfri
09/02/2021, 5:11 PMchannel
class which has a message
property which is a MutableMap<Long, Message>
then I show the list of the messages using the LazyColumn, for that I have no problem. But when I create my channel
, I add to it 20 messages by doing this
for (i in 0..20) {
channel.messages.put(i, Message("test $i"))
}
But in result I get 2-3 messages only, and the numbers are random, sometimes it's 0, 5 and 14, sometimes 5 and 20, and it's very weird and I can't find a way to make it working, what am I doing wrong ?
I'm using the 1.0.0-alpha4-build331 version.hfhbd
09/02/2021, 5:28 PMmutableStateOf()
or mutableStateMapOf()
Ayfri
09/02/2021, 5:40 PMZach Klippenstein (he/him) [MOD]
09/02/2021, 5:55 PMChannel
to your UI to be displayed – is that correct?Channel
doesn't have a messages
property. What is channel
?Ayfri
09/02/2021, 5:56 PMChannel
not related to coroutinesZach Klippenstein (he/him) [MOD]
09/02/2021, 6:00 PMmessages
by creating the map with mutableStateMapOf
instead of mutableMapOf
. Alternatively, messages
could be a MutableState
object that holds an immutable map (i.e. mapOf
), where you're sending a new map in every time you want to add a message.Ayfri
09/02/2021, 6:00 PMZach Klippenstein (he/him) [MOD]
09/02/2021, 6:05 PMAyfri
09/03/2021, 5:30 AMmutableSteMapOf
:(Zach Klippenstein (he/him) [MOD]
09/03/2021, 5:08 PMvar message by remember { mutableStateOf(Message("$text 0", user)) }
for (i in 0..20) {
message = Message("$text $i", user)
channel.messages[message.id] = message
}
This is a bit smelly – creating a mutable state, then mutating that state multiple times in the same composable in a loop. It looks like message
isn't actually state at all and shouldn't be a mutable state or remembered. Is that the only place where you're actually adding items to the message list?interface ITextChannel {
val messages: SnapshotStateMap<Snowflake, Message>
Not related to correctness, but the fact that messages is a SnapshotStateMap
is an implementation detail, I would just make this interface property type MutableMap
insteadAyfri
09/03/2021, 6:05 PMZach Klippenstein (he/him) [MOD]
09/03/2021, 7:10 PMAyfri
09/03/2021, 7:12 PMZach Klippenstein (he/him) [MOD]
09/03/2021, 7:14 PMApp
composable recomposes, you're gonna add another 30 items to the list, but that doesn't explain the ordering…Ayfri
09/03/2021, 7:14 PMZach Klippenstein (he/him) [MOD]
09/03/2021, 7:17 PMmutableStateListOf
)Ayfri
09/03/2021, 7:20 PMZach Klippenstein (he/him) [MOD]
09/03/2021, 7:21 PMAyfri
09/03/2021, 7:22 PMZach Klippenstein (he/him) [MOD]
09/03/2021, 7:22 PMAyfri
09/03/2021, 7:22 PMZach Klippenstein (he/him) [MOD]
09/03/2021, 7:24 PMval now = Clock.System.now()
id = "${now.toEpochMilliseconds()}${now.nanosecondsOfSecond.toString().takeLast(3)}".toLong()
But i'm not sure if Clock.System.now()
will give you nanosecond accuracy – if it doesn't, then you're probably generating duplicate IDs.Ayfri
09/03/2021, 7:25 PMZach Klippenstein (he/him) [MOD]
09/03/2021, 7:27 PMAyfri
09/03/2021, 7:28 PM