Modern Apps git

← Modern-Apps.git

apps: keep lifecycleScope launches off IO on the main thread

Commit
05595da4fdf27b7b42912abe8bdea55e1947d366
Author
Vayun Mathur <[email protected]>
When
2026-11-20 19:23 UTC (15 days ago)
Parents
32dfa06
travel's pending-trip poll queried Room (SELECT * FROM
BookedTrip) inside lifecycleScope.launch on Main - now the
query runs in withContext(Dispatchers.IO) and only the UI-state
handoff stays on Main. web's ShieldsEngine.load ran its
prologue (NativeLoader.load -> System.loadLibrary) on Main
before its own internal IO shift in both MainActivity and
PwaActivity - both now launch on Dispatchers.IO.

cast/tv's three bare launches were audited and left: they
collect an in-memory StateFlow and only touch window/UI state,
which must stay on Main.

Verified: compile for travel, web and cast:tv.

Diff

travel/src/main/java/com/vayunmathur/travel/MainActivity.kt
diff --git a/travel/src/main/java/com/vayunmathur/travel/MainActivity.kt b/travel/src/main/java/com/vayunmathur/travel/MainActivity.ktindex ce26806254..fa116358b5 100644--- a/travel/src/main/java/com/vayunmathur/travel/MainActivity.kt+++ b/travel/src/main/java/com/vayunmathur/travel/MainActivity.kt@@ -51,7 +51,10 @@ class MainActivity : MaActivity() {         // Resume polling any trip that is still awaiting payment / processing.         if (!::repository.isInitialized) return         lifecycleScope.launch {-            viewModel.pendingTripIds().forEach { viewModel.pollTrip(it) }+            // pendingTripIds() reads Room (getAllBookedTrips) — keep that off Main;+            // pollTrip() only touches ViewModel state / launches its own scope, so it stays on Main.+            val ids = withContext(Dispatchers.IO) { viewModel.pendingTripIds() }+            ids.forEach { viewModel.pollTrip(it) }         }     } 
web/src/main/java/com/vayunmathur/web/MainActivity.kt
diff --git a/web/src/main/java/com/vayunmathur/web/MainActivity.kt b/web/src/main/java/com/vayunmathur/web/MainActivity.ktindex f32574a273..17008f0441 100644--- a/web/src/main/java/com/vayunmathur/web/MainActivity.kt+++ b/web/src/main/java/com/vayunmathur/web/MainActivity.kt@@ -50,7 +50,7 @@ class MainActivity : MaActivity() {             ImageLoader.Builder(this).remote { data, _ -> HttpImageFetcher.fetch(data, NetworkClient::getBytes) }.build(),         )         // Parses ~8 MB of filter lists off the main thread. Shields fail open until it lands.-        lifecycleScope.launch { ShieldsEngine.load(applicationContext) }+        lifecycleScope.launch(Dispatchers.IO) { ShieldsEngine.load(applicationContext) }         // Before any WebView exists: service-worker fetches bypass the WebViewClient entirely.         ShieldsServiceWorkerClient.registerOnce(applicationContext) 
web/src/main/java/com/vayunmathur/web/platform/PwaActivity.kt
diff --git a/web/src/main/java/com/vayunmathur/web/platform/PwaActivity.kt b/web/src/main/java/com/vayunmathur/web/platform/PwaActivity.ktindex 75831769dc..d6d989c9f1 100644--- a/web/src/main/java/com/vayunmathur/web/platform/PwaActivity.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/PwaActivity.kt@@ -14,6 +14,7 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.lifecycle.lifecycleScope+import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import com.vayunmathur.library.ui.DynamicTheme import com.vayunmathur.web.platform.shields.ShieldsEngine@@ -46,7 +47,7 @@ class PwaActivity : ComponentActivity() {         enableEdgeToEdge()         // Installed sites get the same shields as the browser; loading is a no-op if the         // engine is already up in this process.-        lifecycleScope.launch { ShieldsEngine.load(applicationContext) }+        lifecycleScope.launch(Dispatchers.IO) { ShieldsEngine.load(applicationContext) }         // A pinned shortcut can be the process entry point without MainActivity ever running.         ShieldsServiceWorkerClient.registerOnce(applicationContext)         handleIntent(intent)