When will it be possible to combine @Composable an...
# compose
r
When will it be possible to combine @Composable and Room ? I think its crucial that those two features can coexist. RG
a
Yes. Room can give you back both `Flow`s and `LiveData`s of query results, and compose can subscribe to those and render the results as they change.
You can do it today with some integration glue
Flow
is a bit of a challenge at the moment due to some incompatibilities between the version of the IR compiler the latest compose plugin is using and generating suspending code, but some clever module separation can work around it for now. We're pretty anxious to see these bugs get fixed soon
LiveData
is a little bit easier, let me see if I can dig up the snippet to use as an
observe()
effect for it
there it is:
Copy code
fun <T> observe(data: LiveData<T>) = effectOf<T?> {
    val result = +state<T?> { data.value }
    val observer = +memo { Observer<T> { result.value = it } }

    +onCommit(data) {
        data.observeForever(observer)
        onDispose { data.removeObserver(observer) }
    }

    result.value
}
usage is:
Copy code
val currentValue = +observe(myLiveData)
you'll get a recomposition each time the
LiveData
value changes, and the
LiveData
will have an active observer for the duration of that composition
which is to say, the `LiveData`s returned by room queries will continue updating until that part of your compose UI goes away
r
Thanx alot !!! @Adam Powell, I'll test this on Monday...
👍 1
a
Just what I needed! Have been working on this over the weekend 🙏🏽
👍 1
a
@Roar Gronmo I used the +observe snippet of code successfully in a few talks I gave on Compose recently, just to show something more heavyweight compared to the existing @Model observable examples.
a
@Andrew Kelly Do you have a link to the talks?
a
I did them at DevFest Melbourne, DevFest Sydney and DevFest Cape Town. I haven’t seen any uploads yet, I imagine they’re still processing them all.