在非挂起函数中调用挂起函数

huangapple go评论67阅读模式
英文:

Call suspend function in non suspend function

问题

当在Android应用程序中使用Kotlin进行实现时,我遇到了这个问题。
我必须覆盖一个存在于aar中的函数,所以我不能更改它。它是一个非暂停函数。

// 来自aar
class A {
    fun doSomething()
}

但我必须覆盖这个函数并调用暂停函数。像这样

class B() : A {
    override fun doSomething() {
        // **我想在这里调用一个暂停函数**
    }
}

我是Kotlin中的新手。据我所知,我可以使用 runBlocking 使其工作。是否有其他关于此的想法?

顺便问一下,我还想知道协程的缺点是什么?

  • Kotlin文档说它将节省内存。我们可以创建尽可能多的协程。

Kotlin协程与其他语言(C ++,Go)有什么不同?

英文:

When using Kotlin in Android app implemention, I met this problem.
I must overwrite a function which is in the aar, so I can not change the it. It is a non suspend function.

// from the aar
class A{
    fun doSomething()
}

But I have to overwrite this function and call suspend function. like this

class B() : A{
    overwrite fun doSomething(){
        // **I want to call a suspend function here**
    }
}

I am a new guy in Kotlin. As I know, I can use runBlocking to let it work.
Is there any other ideas on this?

Btw I also want to ask what is the Cons of the coroutines?

  • Kotlin document said it will save the memory. We can create it as many as we can.

What is the difference of Kotlin corountines with others (C++, Go)?

using runBlock in my codes

答案1

得分: 1

  1. 使用 runBlocking,但它将阻塞您的函数。您的库必须能够处理此函数作为阻塞函数。如果库在主线程上调用此函数,这可能没问题,但如果如此,将会出现问题。

  2. 从此函数中启动一个协程,该协程将以异步方式运行代码。函数会立即返回,而协程会在未来某个时刻完成。如果您不需要等待结果,这个解决方案是可以的。

如果启动协程,请确保使用适当的 CoroutineScope,以避免内存泄漏。阅读 Kotlin 协程文档,了解 CoroutineScope 的含义。基本上,当取消 CoroutineScope 时,仍在运行的所有已启动协程也将被取消。因此,当启动协程时,您希望使用在适当时间被取消的作用域,以避免内存泄漏。例如,在 Android Fragment 中,可以使用 viewLifecycleOwner.lifecycleScope 来启动捕获视图引用的协程,这将防止这些协程泄漏视图。

英文:

You have two choices:

  1. Use runBlocking, but it will block your function. Your library will have to be able to handle this function being a blocking function. It might be fine, but it will be a problem if the library is calling this function on the main thread.

  2. Launch a coroutine from this function, which will run the code asynchronously. The function will return immediately while the coroutine is started asynchronously and will finish some time in the future. This solution is OK if you don't need to wait for the result.

If you launch a coroutine, make sure you use an appropriate CoroutineScope so you don't leak memory. Read the Kotlin coroutines documentation to understand what is the meaning of CoroutineScope. Basically, when a CoroutineScope is cancelled, all its launched coroutines that are still running will also be cancelled. So, when you launch a coroutine, you want to use a scope that will be cancelled at an appropriate time to avoid leaking memory. For example, in an Android Fragment, use the viewLifecycleOwner.lifecycleScope to launch coroutines that capture references to views, and this will prevent those coroutines from leaking views.

答案2

得分: 0

If you use runBlocking, it will block the current thread. However, you can use GlobalScope as below.

使用 runBlocking 会阻塞当前线程。但是,你可以像下面这样使用 GlobalScope。

override fun doSomething(){
    GlobalScope.launch {
        // code here
    }
}

runBlocking
GlobalScope

英文:

If you use runBlocking, it will block the current thread. However, you can use GlobalScope as below.

 overwrite fun doSomething(){
         GlobalScope.launch {
           // code here
        }
     }

runBlocking
GlobalScope

huangapple
  • 本文由 发表于 2023年4月4日 14:33:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定