-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Android LiveData / architecture components integration #258
Comments
@GeoffreyMetais We may help this get real by telling our experience using both |
@LouisCAD Please, do. |
The first thing I'd like to say is about null safety and generics:
However,
I think this would be the first step, but the issue is that using the class name The names I found (for now) are If you don't agree with me, or otherwise, if you have other name ideas, please, tell me. |
+1 for nullability, I'm starting my own
In my current refactoring, I have to set an actor to queue operations and guarantee data integrity |
I agree about @GeoffreyMetais Since you've very recently added VLC Android to GitHub, you can now share permalinks of relevant lines of your A part where I see room for improvement is having a coroutines friendly version of My previous comment reminds me about the fact that Allowing smarter behaviors could be a design consideration in the coroutines implementation. I hope I covered useful things for this issue and it's not too verbose for the target folks 🙂 |
Hi, Me and Andras Nemeth (@mos8502) from Atlassian are working on integration module. Basically we need extension functions to convert Let us do and test prototype and share for initial feedback. Stay tuned. 😉 Thanks for feedback @GeoffreyMetais @LouisCAD. |
Me and Andras wanted to share some progress on coroutines + arch work. Let’s speak about Our initial idea was to wrap 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 Usage sample: val channel = liveData.observeChannel(lifecycleOwner)
launch {
channel.consumeEach { data: List<Data>? ->
// use data
}
} This is a nice way to bridge |
Uploaded to github working android sample. |
Also one thing to note is that It looks like to me like |
@matejdro why would you want the channel to expose wether anybody is subscribed or duplicate LiveData API like
The latest proposal is more close to what |
Well I'm still looking for a good stream/observable implementation with following properties:
Consuming
|
@matejdro I'm note sure what you're describing here is in scope, but then again it depends on what what approach is preferred. The approach we took and described by @dmytrodanylyk is a simple extension to Eliminating the middle man ( But you would have the same problem if a 3rd party library decided to expose observable data as It seem what you are looking for is a new subscribable producer (channel) that only starts producing content when there is at least one subscriber. This is just a very naive implementation and a lot of error cases are not handled (also the name is misleading a bit as
|
Yes I agree that this is out of scope. My post was mostly replying to @GeoffreyMetais and @LouisCAD that talked about replacing |
I like the approach by @dmytrodanylyk for livedata-to-channel integration. Seems clean to me and solves this particular integration issue. As a side note, I feel and see the need for something like @matejdro Is right that missing |
One important thing to note is that all Code from @dmytrodanylyk above would crash if coroutine with any other dispatcher than |
I think we should realy on how There must be reasons why google decided to go with this approach. What if in future they allow to observe on non ui thread? |
@matejdro I think I agree with @dmytrodanylyk here. Even though looking at the source it's clear that there are assertions around the current invoking thread, these are not documented. The only case when it's explicitly being called out that you must call the method on the main thread is Event with the channel "wrapper" you may use Maybe just as RxBinding we could make an assertions ourselves to check if those invocations are made from the correct thread, but if that's already done for us by LiveData I don't see the value in it. Also as @dmytrodanylyk says, if removing an observer is going to be allowed in a future release of LiveData we will have unnecessary restrictions in the implementation |
I'm expanding the scope of this issue to include integration with other Android architecture components that are callback-based. In particular, it seems to be useful to be able to suspend coroutine until a lifecycle reaches a certain state. For example, to animate UI coroutine only in resumed state. See also #104 which I'm closing. |
I'm closing it, as this integration is being picked up to Google and will come from their side. |
@elizarov I'm aware of Google implementing an integration with |
@ashdavies This is likely to become part of livedata-ktx (if a good API can be settled). |
That'd be cool, I'd just assumed since suggested implementations (I'm referencing @dmytrodanylyk's here), make use of |
Not to mention the fact that we still seem to be waiting on a stable release for |
You can make LiveData integration without using internal APIs, you just have to use composition and feed data into channel instead of extending it. |
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 #232The text was updated successfully, but these errors were encountered: