--

The switchMap() operator runs the LiveData creation block on the main thread, yes. But you can use it to enqueue an operation on a backgroud thread, and from that thread publish the value asynchronously to the returned LiveData instance using postValue().

Example:

liveData.switchMap { source ->

val result = MutableLiveData()

val runnable = Runnable {

// some background work

result.postValue(data)

}

backgroundExecutor.execute(runnable)

result

}

--

--

Christophe Beyls
Christophe Beyls

Written by Christophe Beyls

Android developer from Belgium, blogging about advanced programming topics.

No responses yet