android - RunBlocking 与 Dispatcher 通信不起作用

标签 android kotlin dispatcher kotlin-coroutines

I have switched my context to Dispatcher.Main to show UI but data fetched on runBlocking but unable to display in RecylerView

runBlocking {
            var fruits = fetchFruitList()
            withContext(Dispatchers.Main){
                recyclerView.adapter = FruitAdapter(fruits);
            }
        }

我做错了什么以及将数据从一个 Dispatcher 返回到另一个 Dispatcher 的合适方法是什么。

我试过另一种方法

GlobalScope.launch {
            withContext(Dispatchers.IO){
                var fruits = arrayOf("Grapes","Apple","Mango","TuttiFruit","PineApple",
                        "Pomegrante","Apple","Mango","TuttiFruit","PineApple",
                        "Pomegrante","Apple","Mango","TuttiFruit","PineApple").toList()
                return@withContext
            }
            recyclerView.adapter = FruitAdapter(fruits)
        }

但在上述方式中,我必须将 fruits 声明为全局,而我不想让它全局工作。有没有一种方法可以将数据从一个“调度程序队列”返回到另一个“调度程序队列”

I have to fetch data from Api (IO operation) and display that data in RecyclerView(Main Thread Operation)

最佳答案

那是因为你必须在获取数据后切换上下文:

GlobalScope.launch(Dispatchers.IO){
       var fruits = arrayOf("Grapes","Apple","Mango","TuttiFruit","PineApple",
                    "Pomegrante","Apple","Mango","TuttiFruit","PineApple",
                    "Pomegrante","Apple","Mango","TuttiFruit","PineApple").toList()

       withContext(Dispatchers.MAIN){
           recyclerView.adapter = FruitAdapter(fruits)
       }        
}

根据评论编辑:

对于 runBlocking,请查看文档第一段。

Runs a new coroutine and blocks the current thread interruptibly until its completion. This function should not be used from a coroutine. It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in main functions and in tests.

其次,您要求使用GlobalScope。是的,如果你在 Android 中使用协程,你应该避免这种情况。 Reasons here .

如何在 Android Activity/Fragment 中启动协程?

首先,我建议在 ViewModelPresenter 中使用,但如果您想在 Activity/Fragment 中启动协程,则需要一种方法来控制和管理它的取消以避免内存泄漏。 解决方案是:

private val job: Job = Job()
                                              //or Dispatchers.IO
private val fragmentScope = CoroutineScope(Dispatchers.MAIN + job)

//launch a coroutine later in Activity/Fragment

fragmentScope.launch{
  //the default coroutine dispatcher would be the defined dispatcher above
}

override fun onDestroy(){
  super.onDestroy()
  fragmentScope.cancel()
}

关于你的问题:

what am I doing wrong and what is the appropriate way to return data from one Dispatcher to another

如果你想从不同的上下文中返回值,你也可以尝试这个解决方案:

someScope.launch(Dispatchers.MAIN){
 var data = withContext(Dispatchers.IO){
  val someData = fetchSomeData()
  return@withContext data
}
if(data.isAvailable()){ //for example
 //runing on the main thread
 }
}

关于android - RunBlocking 与 Dispatcher 通信不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58553757/

相关文章:

java - 动态更改十六进制整数的 alpha 值

kotlin - 首次运行时未定义的迭代器值

c# - 在 Silverlight 中调用 Dispatcher.CheckAccess() 是好的形式吗?

c# - 我可以使 WinForms OpenFileDialog.ShowDialog 不产生主线程吗?

c# - 如何测试使用 DispatcherTimer 的类?

Android 屏幕方向因设备而异

java - ADT Eclipse 中出现无效字符常量

android - Kotlin 中的构造函数

Android Sync 适配器一直运行

kotlin - 具有随机初始解的旅行推销员,优化算法返回意外结果