Android Kotlin 协程未按预期工作

Android Kotlin coroutines not working as I expected

当我打开我的应用程序时,启动 Activity 在某些设备上崩溃并且它立即在我的设备上消失。 这是我的启动代码Activity:

CoroutineScope(Dispatchers.Main).launch {
        withContext(Dispatchers.IO) {
            dataStore.data.map { preferences ->
                preferences[DataStoreManager.NOT_FIRST_LUNCH] ?: false
            }.collect { notFirstLaunch ->
                if (!notFirstLaunch) {
                    editBoolean(DataStoreManager.NOT_FIRST_LUNCH, true)
                    withContext(Dispatchers.Main) {
                        activityFullscreenCompat()
                        setContentView(R.layout.activity_lunch)
                        prepareIntro()
                        lunch_btn_skip.setOnClickListener { //todo gotoSplash()
                            gotoMain()
                        }
                    }
                } else {
                    withContext(Dispatchers.IO) {
                        dataStore.data.map { preferences ->
                            preferences[DataStoreManager.IS_NIGHT] ?: false
                        }.collect { isNight ->
                            withContext(Dispatchers.Main) {
                                if (isNight)
                                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
                                else
                                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
                                //todo gotoSplash()
                                gotoMain()
                            }
                        }
                    }
                }
            }
        }
    }

花了一点时间后我不明白当我改变布尔值时:

editBoolean(DataStoreManager.NOT_FIRST_LUNCH DataStore 立即更改它并且流发出新值并且在用户点击跳过条件之前再次运行并且这次具有新值并且错误条件变为真(逻辑上)。

相反,我将其添加到点击侦听器中