如何从Java 7调用Kotlin暂停协程函数 [英] How to call Kotlin suspending coroutine function from Java 7
问题描述
我正在尝试从Java 7调用Kotlin函数.我正在使用协程,并且这个被调用的函数正在挂起,例如:
I'm trying to call Kotlin function from Java 7. I'm using coroutines and this called function is suspending, for example:
suspend fun suspendingFunction(): Boolean {
return async { longRunningFunction() }.await()
}
suspend fun longRunningFunction() : Boolean {
delay(400)
return true
}
我在0.25.3版中使用了协程,我可以通过将Continuation<U>
实例作为挂起函数的参数来模拟简单的Java回调样式,例如
I was using coroutines in version 0.25.3 and I could emulate simple Java callback style by passing Continuation<U>
instance as an argument to suspending function, e.g.
CoroutinesKt.suspendingFunction(new Continuation<Boolean>() {
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}
@Override
public void resume(Boolean value) {
doSomethingWithResult(value);
}
@Override
public void resumeWithException(@NotNull Throwable throwable) {
handleException(throwable);
}
});
但是,在更新到完全稳定的1.0.1版本之后,我认为它不再可行.假设暂停功能的更新版本如下:
However, after updating to fully stable 1.0.1 release, I think it's no longer possible. Let's say updated version of suspending function looks like that:
suspend fun suspendingFunction(): Boolean {
return GlobalScope.async { longRunningFunction() }.await()
}
Continuation<U>
现在使用Result
类,这在Java中似乎无法使用(这是内联类,因此很有意义).我试图使用协程的某些Continuation
子类,但它们都是内部的或私有的.
Continuation<U>
now uses Result
class, which seems to be unusable from Java (which makes sense as it is inline class). I was trying to use some subclass of Continuation
from coroutines but they are all internal or private.
我知道通常是建议将协程转换为CompletableFuture
,但我使用的是Android,这意味着仅Java 7.另一方面,简单的Future
太笨了,因为我不想定期检查函数是否完成-我只想在函数完成时被调用.而且我真的很想避免添加新的库或许多其他的类/方法.
I know that usually it is advised to transform coroutine to CompletableFuture
, but I'm on Android, which means Java 7 only. Simple Future
on the other hand is too dumb as I don't want to check periodically if function is finished - I just want to be called when it is finished. And I would really like to avoid adding new libraries or many additional classes/methods.
有没有简单的方法可以直接从Java 7中调用暂停功能?
当Kotlin试图与Java互操作性很强时,我会想到会有一些简单的方法来做到这一点,但是我还没有找到它.
As Kotlin tries to be very interoperable with Java I would imagine there would be some easy way to do that, but I'm yet to find it.
推荐答案
根据您的环境,您有几种选择.
You have several options depending on your environment.
- 如果在项目中使用
RxJava2
,则模块kotlinx-coroutines-rx2
具有实用程序功能,可以在协程和Rx数据类型之间来回转换.
- If you are using
RxJava2
in the project, the modulekotlinx-coroutines-rx2
has utility functions to convert back and forth between coroutines and Rx datatypes.
示例
suspend fun sayHello(): String {
delay(1000)
return "Hi there"
}
fun sayHelloSingle(): Single<String> = GlobalScope.rxSingle { sayHello() }
- 否则,您可以添加一个新的
Continuation
类,该类与旧类的定义匹配,并且在Java方面也很有用.
- Otherwise, you could add a new
Continuation
class that matches the definition of the old one and is also useful in the Java side.
示例(科特林一侧)
abstract class Continuation<in T> : kotlin.coroutines.Continuation<T> {
abstract fun resume(value: T)
abstract fun resumeWithException(exception: Throwable)
override fun resumeWith(result: Result<T>) = result.fold(::resume, ::resumeWithException)
}
示例(Java端)
sayHello(new Continuation<String>() {
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}
@Override
public void resume(String value) {
doSomethingWithResult(value);
}
@Override
public void resumeWithException(@NotNull Throwable throwable) {
doSomethingWithError(throwable);
}
});
这篇关于如何从Java 7调用Kotlin暂停协程函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!