Unresolved reference async in Kotlin

后端 未结 3 1993
情歌与酒
情歌与酒 2021-01-04 03:22

I am trying to perform network operation async in Kotlin. I read it you can do async using async function. I am getting below error, can anyone gue

相关标签:
3条回答
  • 2021-01-04 03:54

    Different Kotlin libraries can have different implementations of async that do different things.

    If you wanted the general async-await function from the core library, make sure you have a dependency to kotlinx.coroutines

    0 讨论(0)
  • 2021-01-04 03:56

    async is available inside kotlinx.couroutines

    Make sure that you added the dependency in your gradle file:

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
    

    Also make sure that the async is called on a coroutine scrope such as GlobalScope.

     val deferredResult = GlobalScope.async {
    
                }
    
    0 讨论(0)
  • 2021-01-04 04:10

    I see async function is available in anko library not Kotlin library itself. https://github.com/Kotlin/anko

    I resolved by adding this dependency in build.gradle compile "org.jetbrains.anko:anko-commons:0.10.1" as this is not available in Kotlin itself.

    I see that async is deprecated now, library suggests to use doAsync instead.

    doAsync {
        val forecastWeather = ForecastRequest("302015").execute()
        uiThread {
            Log.d("Test", forecastWeather.toString())
        }
    }
    
    0 讨论(0)
提交回复
热议问题