如何在Coroutine范围中返回价值?
是否可以在coroutine范围
中返回值,而无需运行阻塞? 目前,我在存储库中的代码看起来像这样:
suspend fun getWorkItem(workItemId: Int): WorkItemRoom? {
runBlocking {
return@runBlocking
CoroutineScope(Dispatchers.Main).launch {
getWorkItemByIdUseCase.build(workItemId)
}
}
return null
}
这是我的用户录取
class GetWorkItemByIdUseCase(private val workItemDao: WorkItemDao) :
BaseUseCase<Int, WorkItemRoom>() {
override suspend fun create(id: Int): WorkItemRoom {
return workItemDao.getWorkItemById(id)
}
}
碱基cassecase
abstract class BaseUseCase<P, R> {
protected abstract suspend fun create(params: P): R
open suspend fun build(params: P): R = create(params)
}
dao
@Dao
abstract class WorkItemDao {
@Query("SELECT * FROM workitem WHERE id=:id")
abstract suspend fun getWorkItemById(id: Int): WorkItemRoom
}
...但是我当然知道这不是一个正确的解决方案。您将如何实现这一目标?在viewModels'或片段中,我可以直接使用
lifecyclescope“,但是在其他情况下,必须直接从下面的方法调用USECase。一直致电调度员是否有效?
CoroutineScope(Dispatchers.Main).launch { }
Is it possible to to return value in Coroutine Scope
without run blocking?
For now my code in repository looks like this:
suspend fun getWorkItem(workItemId: Int): WorkItemRoom? {
runBlocking {
return@runBlocking
CoroutineScope(Dispatchers.Main).launch {
getWorkItemByIdUseCase.build(workItemId)
}
}
return null
}
this is my useCase
class GetWorkItemByIdUseCase(private val workItemDao: WorkItemDao) :
BaseUseCase<Int, WorkItemRoom>() {
override suspend fun create(id: Int): WorkItemRoom {
return workItemDao.getWorkItemById(id)
}
}
baseUseCase
abstract class BaseUseCase<P, R> {
protected abstract suspend fun create(params: P): R
open suspend fun build(params: P): R = create(params)
}
Dao
@Dao
abstract class WorkItemDao {
@Query("SELECT * FROM workitem WHERE id=:id")
abstract suspend fun getWorkItemById(id: Int): WorkItemRoom
}
... but certainly I know it is not a proper solution. How would you achieve this? In viewmodels' or fragments I can directly use
lifecycleScope`, but what in other cases, where the must is to call useCase directly from method below. Is it efficient to call Dispatchers.Main all the time?
CoroutineScope(Dispatchers.Main).launch { }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将值从Coroutine传递给Livedata,然后使用观察者
,然后在Coroutine中进行:
然后在活动中:
You could just pass the value from a coroutine to liveData and then use an observer
and later in a coroutine:
and then in an activity:
在悬挂功能中使用
Runblocking
是没有意义的。 (在一个项目中部分转换为使用Coroutines的项目中,除了作为Coroutines和非coroutine代码之间的桥梁之外,完全使用它几乎没有任何意义,但仍然需要支持遗留代码或库。)你需要。
如果您需要指定调度程序,请使用 使用
:
但是,如果
构建
是暂停功能,则无需在调用时指定调度程序。暂停功能负责内部调用其在适当的线程/调度器上的功能。如果您需要在Coroutine或悬挂功能内部的coroutine范围,请使用小写
coroutinescope
函数,该功能会创建一个范围,如果取消Coroutine,该范围将自动取消。这个示例没有多大意义,因为通常您不需要新的范围,除非您在其中运行并行作业:It doesn't make sense to use
runBlocking
in a suspend function. (It hardly ever makes sense to use it at all, except as a bridge between coroutines and non-coroutine code in a project that is partially converted to using coroutines but still needs to support legacy code or libraries.)You should just call the function you need.
If you need to specify a dispatcher, use
withContext
:However, if
build
is a suspend function, there's no need to specify a Dispatcher when calling it. Suspend functions are responsible for internally calling their functions on appropriate threads/dispatchers.If you need a coroutine scope inside a coroutine or suspend function, use the lowercase
coroutineScope
function, which creates a scope that will be automatically cancelled if the coroutine is cancelled. This example doesn't make much sense, because normally you don't need a new scope unless you are running parallel jobs inside it:您是否曾经听过 lambda ?
它看起来像
呼叫:( myResult) - &gt;单位
我不时使用它
Have you ever listened about a lambda?
It looks like
call: (MyResult) -> Unit
I use it from time to time like