如何从Java 7调用Kotlin暂停协程函数

时间:2019-01-03 13:06:33

标签: java android kotlin coroutine kotlinx.coroutines

我正在尝试从Java 7调用Kotlin函数。我正在使用协程,并且这个被调用的函数正在挂起,例如:

suspend fun suspendingFunction(): Boolean {
    return async { longRunningFunction() }.await()
}

suspend fun longRunningFunction() : Boolean {
    delay(400)
    return true
}

我在0.25.3版中使用了协程,我可以通过传递Continuation<U>实例作为暂挂函数的参数来模拟简单的Java回调样式,例如

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版本之后,我认为它不再可行。假设暂停功能的更新版本如下:

suspend fun suspendingFunction(): Boolean {
    return GlobalScope.async { longRunningFunction() }.await()
}

Continuation<U>现在使用Result类,这在Java中似乎无法使用(这是内联类,因此很有意义)。我试图使用协程的Continuation的某些子类,但它们都是内部的或私有的。

我知道通常是advised to transform coroutine to CompletableFuture,但我使用的是Android,这意味着仅Java 7。另一方面,简单的Future太笨了,因为我不想定期检查函数是否完成-我只想在函数完成时被调用。而且我真的很想避免添加新的库或许多其他的类/方法。

有什么简单的方法可以直接从Java 7中直接调用暂停功能?

当Kotlin尝试与Java进行高度互操作时,我想会有一种简单的方法可以做到这一点,但我还没有找到它。

1 个答案:

答案 0 :(得分:3)

根据您的环境,您有几种选择。

  1. 如果您在项目中使用RxJava2,则模块kotlinx-coroutines-rx2具有实用程序功能,可以在协程和Rx数据类型之间来回转换。

示例

suspend fun sayHello(): String {
    delay(1000)
    return "Hi there"
}

fun sayHelloSingle(): Single<String> = GlobalScope.rxSingle { sayHello() }
  1. 否则,您可以添加一个新的Continuation类,该类与旧类的定义匹配,并且在Java方面也很有用。

示例(科特林一侧)

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);
    }
});