MutableStateFlow不会重新组合值。

huangapple go评论64阅读模式
英文:

MutableStateFlow does not recomposes value

问题

override fun onGameStartOpenWord(startOpenWord: StartOpenWord) {
    var word = _state.value.gameState!!.words.find { startOpenWord.word.id == it.id } ?: return
    val tempCopy = word.copy()
    word = word.copy(
            animationStart = startOpenWord.word.times.first().toULong(),
            animationEnd = startOpenWord.word.times.last().toULong()
        )
    val words = _state.value.gameState!!.words.toMutableList()
    val index = words.indexOf(tempCopy)
    logger.debug("Animation should start at ${word.animationStart}, updating word ${tempCopy.id} with index $index")
    words[index] = word
    val gameState = _state.value.gameState?.copy(words = words)
    _state.update { it.copy(gameState = gameState) }
    logger.debug(_state.value.gameState!!.words[index].animationStart)
}
英文:

I have a method that updates local value if I got a spicific json:

override fun onGameStartOpenWord(startOpenWord: StartOpenWord) {
    var word = _state.value.gameState!!.words.find { startOpenWord.word.id == it.id } ?: return
    val tempCopy = word.copy()
    word = word.copy(
            animationStart = startOpenWord.word.times.first().toULong(),
            animationEnd = startOpenWord.word.times.last().toULong()
        )
    val words = _state.value.gameState!!.words.toMutableList()
    val index = words.indexOf(tempCopy)
    logger.debug("Animation should start at ${word.animationStart}, updating word ${tempCopy.id} with index $index")
    words[index] = word
    val gameState = _state.value.gameState?.copy(words = words)
    _state.update { it.copy(gameState = gameState) }
    logger.debug(_state.value.gameState!!.words[index].animationStart)
}

The State created like this:

private val _state = reset() //MutableStateFlow<GameData>
val state = _state.asStateFlow()

Using state like this:

val model = ViewModelsStore.mainFrameViewModel //compose-jb does not have method viewModel<>(), using object to store
val data by model.state.collectAsState()
val stateWord = data.gameState!!.words.find { it.id == word.id }!!

val animationStart = stateWord.animationStart
val animationEnd = stateWord.animationEnd

if (animationStart != null) {
    //do animation stuff
}

Everything else updates correctly (recomposition starts), but the method above does not. However _state does actually update, logs show that clearly.
Debbuger showed me that before update() method gameState != _state.value.gameState, but after update() it is.

But recomposition didn't happen and animation didn't start. What is the problem?

答案1

得分: 0

小心处理监听器。这是我的错误,gameState 被监听器覆盖了。因此,重新组合没有发生,因为值变化得太快。

英文:

Be careful with listeners. It was my mistake and gameState was overwritten by listener. So recompose didn't happen, because value changed too fast.

huangapple
  • 本文由 发表于 2023年2月18日 21:39:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定