Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate Kotlin from 1.2 to Kotlin 1.3.0 then using async, UI, and bg in a presenter function

I am using MVP pattern on a Kotlin Project. I have a Presenter class:

import com.google.gson.Gson
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
import org.jetbrains.anko.coroutines.experimental.bg

class TeamsPresenter(private val view: TeamsView,
                     private val apiRepository: ApiRepository,
                     private val gson: Gson
) {
    fun getTeamList(league: String?) {
        view.showLoading()

        async(UI){
            val data = bg {
                gson.fromJson(apiRepository
                    .doRequest(TheSportDBApi.getTeams(league)),
                    TeamResponse::class.java
                )
            }
            view.showTeamList(data.await().teams)
            view.hideLoading()
        }
    }   
}

this presenter class working fine on Kotlin 1.2.71, but I can't get it working on Kotlin 1.3.0.

I updated Kotlin version in project's build.gradle, removed "experimental coroutines" and added kotlin coroutine core dependency:

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0'

and this is my current code:

import com.google.gson.Gson

class TeamsPresenter(private val view: TeamsView,
                     private val apiRepository: ApiRepository,
                     private val gson: Gson
) {
    fun getTeamList(league: String?) {
        view.showLoading()

        async(UI){
            val data = bg {
                gson.fromJson(apiRepository
                    .doRequest(TheSportDBApi.getTeams(league)),
                    TeamResponse::class.java
                )
            }
            view.showTeamList(data.await().teams)
            view.hideLoading()
        }
    }
}

Error mainly on async, UI, and bg function:

unresolved reference: async
unresolved reference: UI
unresolved reference: bg

How can I get this to work on Kotlin 1.3.0? for any help, thanks in advance.

like image 536
Dika Avatar asked Nov 02 '18 20:11

Dika


People also ask

How do I update the Kotlin plugin to a new version?

Learn more about types of Kotlin releases and their compatiblity. IntelliJ IDEA and Android Studio suggest updating to a new release once it is out. When you accept the suggestion, it automatically updates the Kotlin plugin to the new version. You can check the Kotlin version in Tools | Kotlin | Configure Kotlin Plugin Updates.

What's new in Kotlin/native?

Kotlin/Native: KDoc export to Objective-C headers and faster Array.copyInto () inside one array Gradle: caching of annotation processors' classloaders and support for the --parallel Gradle property What's new in Kotlin 1.5.20

How to change the code style of a project in Kotlin?

Having the kotlin.code.style option set may modify the code style scheme during a project import and may change the code style settings. After updating your code style settings, activate Reformat Code in the project view on the desired scope.

What is the current version of kotlinx?

kotlinx.html version: 0.7.2 The versions of libraries from kotlin-wrappers (such as kotlin-react) can be found in the corresponding repository. A bug fix release for Kotlin 1.5.30.


1 Answers

you must use GlobalScope.launch instead of launch ,GlobalScope.async instead of async Dispatcher.main instead of UI

coroutineBasics

like image 170
Kourosh Avatar answered Sep 26 '22 02:09

Kourosh