Tag: 协同

CORUTTINE_SUSPENDED和suspendCoroutineOrReturn在Kotlin

kotlin中的协程的概念是抽象暂停和回调的概念,并写出简单的顺序代码。 如果协程暂停或不在线,则无需担心,与线程类似。 suspendCoroutineOrReturn和COROUTINE_SUSPENDED的用途是什么,在什么情况下你会使用它们?

在Kotlin协同程序中暂停function是什么意思

我正在阅读Kotlin Coroutine,知道它是基于suspendfunction的。 但suspend是什么意思? 协程或函数被暂停? 从https://kotlinlang.org/docs/reference/coroutines.html 基本上,协程是可以暂停而不阻塞线程的计算 我听到有人经常说“暂停function”。 但是我认为是因为等待函数完成而被暂停的协程? “挂起”通常意味着“停止运行”,在这种情况下,协程是空闲的。 🤔我们应该说协程暂停了吗? 哪个协程暂停? 从https://kotlinlang.org/docs/reference/coroutines.html 为了继续这个比喻,await()可以是一个挂起函数(因此也可以从async {}块中调用)暂停一个协程,直到某个计算完成并返回结果: async { // Here I call it the outer async coroutine … // Here I call computation the inner coroutine val result = computation.await() … } 🤔它说“暂停一个协程,直到一些计算完成”,但协程就像一个轻量级的线程。 所以如果协程暂停,那么计算如何完成呢? 我们看到await被调用computation ,所以它可能是async ,返回Deferred ,这意味着它可以启动另一个协程 fun computation(): Deferred { return async { true } […]

在使用Kotlin协同程序时,与Room dao类错误

我试图使用kotlin协同程序通过这里描述的方法访问房间数据库,添加了插件和依赖项,并启用了gradle中的kotlin协程。 在gradle文件中: kotlin { experimental { coroutines ‘enable’ } } dependencies { implementation “org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21” …} 所以我为dao类中的所有方法添加了suspend关键字,如下所示: 道class @Query(“select * from myevent”) suspend fun all(): List @Delete suspend fun deleteEvent(event: MyEvent) … 并建立,然后得到这些错误 错误 e: C:\Users\projectpath\app\build\tmp\kapt3\stubs\debug\com\robyn\myapp\data\source\local\EventsDao.java:39: error: Deletion methods must either return void or return int (the number of deleted rows). public abstract java.lang.Object deleteEventById(@org.jetbrains.annotations.NotNull() ^ […]