Skip to content
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

How to Combine(zip) 2 or more API calls in LiveData #40

Closed
mssaravanan opened this issue Jun 1, 2017 · 11 comments
Closed

How to Combine(zip) 2 or more API calls in LiveData #40

mssaravanan opened this issue Jun 1, 2017 · 11 comments

Comments

@mssaravanan
Copy link

In Reactive java we use observable.zip() for combine n numbers of API call or methods result into one .
In LiveData , How we achive this , Any sample code is avilable ?

@NkoroiEric
Copy link

I don't have code yet but I think you can still achieve the same by using LiveData fromPublisher (Publisher publisher). In this case publisher being flowable.zip()

@mssaravanan
Copy link
Author

mssaravanan commented Jun 1, 2017 via email

@amaksoft
Copy link

amaksoft commented Jun 1, 2017

Hi!
You have to add
compile "android.arch.lifecycle:reactivestreams:1.0.0-alpha1" // RxJava <-> LiveData
to dependencies

@yigit
Copy link
Contributor

yigit commented Jun 3, 2017

We don't provide these methods because we don't want live data to grow into the complexity of RxJava. That is why we provide the reactivestreams API for RxJava people.

Alternatively, you can implement this yourself using the MediatorLiveData class. It automatically handles passing down lifecycle so that calculations can pause if there are no active observers.
Check the implementation of Transformations class for details.

@yigit yigit closed this as completed Jun 3, 2017
@mssaravanan
Copy link
Author

mssaravanan commented Jun 7, 2017

I have add ,
compile "android.arch.lifecycle:reactivestreams:1.0.0-alpha1"
I got gradle build error :
Error:Failed to resolve: Could not resolve android.arch.lifecycle:reactivestreams:1.0.0-alpha1.
Required by: project :app

@NkoroiEric
Copy link

NkoroiEric commented Jun 7, 2017

Hi man checkout this link for an explanation https://developer.android.com/topic/libraries/architecture/adding-components.html or you can simply add
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
}
}
to build.gradle file so as to get the arch dependencies from googles maven repo.

@AkshayChordiya
Copy link

AkshayChordiya commented Aug 22, 2017

@yigit I agree with you. I think more complex transformations can be added in the Transformations class to make it easier building implementation similar to zip.

@mssaravanan
Copy link
Author

@yigit , I was trying your method MediatorLiveData ,it only handle same return type of LiveData not different type LiveData. I tried a lot , but didn't find correct approach to combine the call's using MediatorLiveData and Transfermations.

@magneticflux-
Copy link

Here's the Kotlin code I used to implement my zip function, along with useful extension functions: https://gist.github.com/magneticflux-/044c9d7a3cea431aa0e4f4f4950a2898

@ch4vi
Copy link

ch4vi commented May 29, 2018

I had the same needs and at the end I solved with a Kotlin extension that allows me to concatenate livedata results as I did with promises. I created a gist with my solution https://gist.github.com/ch4vi/0f7893bd830f195a99881a30b3cb7640

@Antonkog
Copy link

Antonkog commented Oct 8, 2020

Create class that contains child and parent room Entity from: https://developer.android.com/reference/androidx/room/Transaction

/**
* Created by Anton Kogan on 10/7/2020
*/
data class TaskWithActivities(@Embedded var taskEntity: TaskEntity, @Relation(
   parentColumn = "taskId",//id in child Entity class
   entityColumn = "taskpId"//same id in parent  Entity class
) var activities : List<ActivityEntity>) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants