I have the strangest bugs again. I have a `Mutable...
# compose
v
I have the strangest bugs again. I have a
MutableState
and two nested derived states reading it. I read the deepest-nested derived state with
snapshotFlow
but it doesn't get updates. What is wrong? I'm running out of theories... 🧵
Copy code
CoroutineScope(Dispatchers.Main).launch {
    val truth = mutableStateOf(true)
    val derived1 = derivedStateOf { truth.value }
    val derived2 = derivedStateOf { derived1.value }

    launch {
        snapshotFlow { derived2.value }.collect {
            Log.d("wat", "job1: $it")
        }
    }

    launch {
        snapshotFlow { derived2.value }.collect {
            Log.d("wat", "job2: $it")
        }
    }

    delay(100)
    truth.value = false
}
I would expect to see
job1: true
job2: true
job1: false
job2: false
In reality I see > job1: true > job2: true > job1: false
s
Interesting, that sounds like a caching bug to me
v
If it passes your sanity check on something that should definitely work, I guess it really could be a bug. I can file on the tracker
z
what happens if you wrap your
truth.value = false
in a
withMutableSnapshot
?
i’m guessing it won’t fix it but there was another discussion this week about a race condition here so wondering if it’s related
v
yeah, same result
@shikasd thanks for taking a look, we really appreciate it! I don't really understand the fix though, should probably dive deeper to be able to debug these. Are there any public docs on how snapshots work apart from in-code comments?
s
well you don't have to understand it, tbh, it was caused by somewhat weird interaction of nested derived states and a snapshot flow I think Zach's articles are the best source of info we have: https://dev.to/zachklipp/introduction-to-the-compose-snapshot-system-19cn
z
Unfortunately I don’t have one on derived state… yet. I’ll be giving a talk at droidcon sf about it, planning to publish a blog post version after as well