1

The following code doesn't compile, although the documentation says it should be as simple as this:

override fun onResume() {
    super.onResume()
    async {
        Log.d("foo", "async")
    }
}

The error is:

...kt: (31, 9): None of the following functions can be called with the arguments supplied:
@Deprecated public fun <T> async(context: CoroutineContext, start: Boolean, block: suspend CoroutineScope.() -> ???): Deferred<???> defined in kotlinx.coroutines.experimental
public fun <T> async(context: CoroutineContext, start: CoroutineStart = ..., block: suspend CoroutineScope.() -> ???): Deferred<???> defined in kotlinx.coroutines.experimental
user2297550
  • 3,142
  • 3
  • 28
  • 39

4 Answers4

2

If you compare signatures listed with https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/async.html, it's very different.

It looks like you are using an old version of coroutines library, and in particular one where context has no default value.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
2

In order to use coroutines in an android app it is a must to add a dependency to kotlinx-coroutines-android as described here.

user2297550
  • 3,142
  • 3
  • 28
  • 39
0

Also, async function can only be used inside a coroutine or suspend function. So even if you have the correct dependencies, if you try to use async in onResume(), your code cannot compile.

Alison Z
  • 85
  • 1
  • 8
-3

the async{} method is deprecated from Kotlin 1.1 try:

override fun onResume() {
    super.onResume()
    doAsync {
        Log.d("foo", "async")
    }
}

I suggest to you to read this question

Trusted
  • 225
  • 1
  • 4
  • 14
  • NOT deprecated, see the 3rd line of the error in my question; also see the official doc: https://kotlinlang.org/docs/reference/coroutines.html – user2297550 Jun 18 '18 at 08:21
  • It's not deprecated. I have no clue where you see that. yes, the async method in Kotlin anko is deprecated, **but not the one in Kotlin coroutines** – Zoe Jun 18 '18 at 14:49