question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android LiveData / architecture components integration

See original GitHub issue

We need an integration module for Android LiveData architecture components that provides read-to-use extensions for their use with coroutine in a similar style to reactive integration modules. See also #232

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:58
  • Comments:26 (13 by maintainers)

github_iconTop GitHub Comments

16reactions
dmytrodanylykcommented, Apr 6, 2018

Uploaded to github working android sample.

12reactions
dmytrodanylykcommented, Mar 23, 2018

@elizarov

Me and Andras wanted to share some progress on coroutines + arch work. Let’s speak about LiveData first, as it is most used component which is used in Room and ViewModel.

Our initial idea was to wrap LiveData with ConflatedBroadcastChannel which basically act the same. But implementation is error prone and may not reflect all the future changes which google may make.

So we decided to implement it in different way.

import android.arch.lifecycle.Observer
import android.arch.lifecycle.LifecycleObserver

class LiveDataChannel<T>(private val liveData: LiveData<T>)
    : LinkedListChannel<T?>(), SubscriptionReceiveChannel<T?>, Observer<T?>, LifecycleObserver {

    override fun onChanged(t: T?) {
        offer(t)
    }

    override fun afterClose(cause: Throwable?) = liveData.removeObserver(this)

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestroy() = close()

}

fun <T> LiveData<T>.observeChannel(lifecycleOwner: LifecycleOwner): LiveDataChannel<T> {
    val channel = LiveDataChannel(this)
    observe(lifecycleOwner, channel)
    lifecycleOwner.lifecycle.addObserver(channel)
    return channel
}

fun <T> LiveData<T>.observeChannel(): LiveDataChannel<T> {
    val channel = LiveDataChannel(this)
    observeForever(channel)
    return channel
}

Basically you subscribe to LiveData but as a return type get Channel, not an Observable.

Usage sample:

val channel = liveData.observeChannel(lifecycleOwner)
launch {
    channel.consumeEach { data: List<Data>? ->
        // use data
    }
}

This is a nice way to bridge LiveData - Channels.

Read more comments on GitHub >

github_iconTop Results From Across the Web

LiveData overview - Android Developers
LiveData in an app's architecture ... LiveData is lifecycle-aware, following the lifecycle of entities such as activities and fragments. Use ...
Read more >
Bind layout views to Architecture Components
Bind layout views to Architecture Components · Use LiveData to notify the UI about data changes · Use ViewModel to manage UI-related data...
Read more >
Use Kotlin coroutines with lifecycle-aware components
Lifecycle-aware components provide first-class support for coroutines for logical scopes in your app along with an interoperability layer ...
Read more >
Handling Lifecycles with Lifecycle-Aware Components
LiveData , a lifecycle-aware component, allows your app to automatically update the UI when your user changes locations. Stopping and starting ...
Read more >
Use LiveData with ViewModel - Android Developers
In this codelab, you learn how to use LiveData, one of the Architecture components. Convert the app data in the ViewModel to LiveData....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found