Search code examples
androidkotlinandroid-livedatakotlin-coroutinesswitchmap

Is there, in kotlin and android, something similar to switchmap in rxjs or rxjava?


I want to find out is there something similar to switchmap in kotlin and android (LiveData maybe).

I need to change my request immediately after events that can happen very often and which determine what to request from the server, respectively, I only need the last request


Solution

  • In Kotlin you can use the Transformations library as follows

    val someLiveData: LiveData<Type> = ...
    
    val someOtherValue = Transformations.switchMap(someLiveData) { changedValue: Type ->
        //Assign someOtherValue using changedValue, the value coming from someLiveData.
    }