简体   繁体   English

我对 android studio 中的生命周期范围的工作方式感到困惑

[英]I am confused with how lifecycleScope in android studio works

I am confused about how flow.collect works.我对 flow.collect 的工作方式感到困惑。 Because in the lifecycleScope below I already say that a should be assigned by the value of data in my database.因为在下面的生命周期范围中,我已经说过 a 应该由我的数据库中的数据值分配。 However, the value of a is still the string of "Hi" instead of "Hello".但是,a 的值仍然是字符串“Hi”而不是“Hello”。

class MainActivity : AppCompatActivity() {
    private var binding: ActivityMainBinding? = null
    private var a: String = "Hi"

override fun onCreate(savedInstanceState: Bundle?) {
    binding = ActivityMainBinding.inflate(layoutInflater)
    super.onCreate(savedInstanceState)
    setContentView(binding?.root)

    val somethingDao = SomethingDatabase.getDatabase(this).somethingDao()

    lifecycleScope.launch {
        somethingDao.insert(SomethingModel("Hello"))
        somethingDao.fetchAllSomething().collect {
            a = it[it.size - 1].name
        }
    }

    println(a)

}
}

this is all of the information in my database这是我数据库中的所有信息

在此处输入图像描述

lifecycleScope.launch will start a coroutine, to make it simple the code inside lifecycleScope.launch will be executed in another thread and it will take some time until inserting data and reading it from database, but println(a) is on the main thread so it will be executed before this line a = it[it.size - 1].name , so your println(a) should be inside lifecycleScope.launch like this: LifecycleScope.launch 将启动一个协程,为简单起见,lifecycleScope.launch 中的代码将在另一个线程中执行,插入数据并从数据库中读取数据需要一些时间,但println(a)在主线程上,所以它将在此行之前执行a = it[it.size - 1].name ,因此您的println(a)应该像这样在生命周期范围内:

class MainActivity : AppCompatActivity() {
    private var binding: ActivityMainBinding? = null
    private var a: String = "Hi"

    override fun onCreate(savedInstanceState: Bundle?) {
        binding = ActivityMainBinding.inflate(layoutInflater)
        super.onCreate(savedInstanceState)
        setContentView(binding?.root)

        val somethingDao = SomethingDatabase.getDatabase(this).somethingDao()

        lifecycleScope.launch {
            somethingDao.insert(SomethingModel("Hello"))
            somethingDao.fetchAllSomething().collect {
                a = it[it.size - 1].name
                println(a)
            }
        }
    }
}

Note: take a look on kotlin coroutines to better understand注意:查看 kotlin 协程以更好地理解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我对 android 权限感到困惑 - I am confused by android permission 如何在Android中读取此JSON? 我对jsonobject和json字符串感到困惑 - How to read this json in android? I am confused with jsonobject and json string 我很困惑 android 中的 ffmpeg 文件是什么 - I am confused what ffmpeg file is in android 对Android加密的工作方式感到困惑 - Confused about how Android Encryption works 为什么我在 Java Android 中没有得到生命周期范围? - Why does I'm not getting lifecycleScope in Java Android? 如何在 WebSocketListener 中使用暂停 function 或 viewModelScope/lifecycleScope? - How can I use suspend function or viewModelScope/lifecycleScope in WebSocketListener? Android布局,表格,相对和线性,我对它们的差异感到困惑 - Android Layouts, Table, Relative and Linear, i am confused in their differences 谁能告诉我有关Android的InternalStorage的信息? 我很困惑 - Can anyone tell me about InternalStorage in Android ? I am confused 我很困惑如何处理 Android Studio 中的 xml 代码无法处理 Java 代码 [暂停] - I'm confused how to deal with the xml code in Android Studio can't deal with Java code [on hold] 这是我在 Android Studio 中获得的设备类型。 如何修复 - This is the type of device I am getting in Android studio. How to fix it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM