cashapp / molecule

Build a StateFlow stream using Jetpack Compose
https://cashapp.github.io/molecule/docs/1.x/
Apache License 2.0
1.78k stars 76 forks source link

RecompositionMode.Immediate with a non-main dispatcher cause ANR while scrolling a LazyColumn (Android) #396

Closed j2esu closed 3 months ago

j2esu commented 4 months ago

According to the docs RecompositionMode.Immediate can be used "for running molecules off the main thread". When I try to use it with any non-main dispatchers (tried Default, IO, new single thread dispatcher) my app freezes and then gives ANR when I scroll any LazyColumn. I don't even use the result of molecule to render the list, just any call to molecule affects lazy lists that way. Please let me know if I'm doing something wrong.

Molecule 1.4.1 Compose compiler 1.5.10 Compose UI 1.6.2 Android SDK 34

MRE:

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifecycleScope.launchMolecule(RecompositionMode.Immediate, Dispatchers.Default) { }
        setContent {
            LazyColumn(modifier = Modifier.fillMaxSize()) {
                items(100) {
                    Text(text = "Item $it", modifier = Modifier.padding(16.dp))
                }
            }
        }
    }
}
JakeWharton commented 4 months ago

Yeah I suspect this is due to the snapshot system and both us and Compose UI launching a coroutine to apply snapshots periodically, but would have to confirm that. It also means that we likely can reproduce this in pure Compose (i.e., no Molecule) and potentially file a bug upstream.

JakeWharton commented 4 months ago

I do not have time to look at this any time soon. If no one else does, I'll get to it eventually.

jingibus commented 3 months ago

This has also hit us pretty hard at Cash. We're raising the issue with upstream.

JakeWharton commented 3 months ago

It also means that we likely can reproduce this in pure Compose (i.e., no Molecule) and potentially file a bug upstream.

https://issuetracker.google.com/issues/329011032

j2esu commented 3 months ago

Tested with androidx.compose.runtime:runtime:1.7.0-alpha06. Looks fixed to me, no more ANRs.