gpt4 book ai didi

android - RunBlocking 与 Dispatcher 通信不起作用

转载 作者:行者123 更新时间:2023-11-29 18:26:41 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com