I want to call a function twice with different parameters and the function returns set of values based on parameters. I want to collate both results and do something with it.
I looked up and I should be using flatmap to do that, but I am not sure how to.can you guide me on it please.
getCompaniesData(pageNumber, perPage) // returns 100 companies
getCompaniesData(pageNumber, perPage) //returns 100 companies
Collate both responses - total 200 companies
Do something with it.
Currently this is what I have which returns with params (1,100), Once I get the data I want to call the same function with params (2,100) which gives me another set of data and combine them both and do something with them
mHighLightsPresenter. getCompaniesData(1, 1000).doOnNext(fetchCompaniesResponse -> {
if(fetchCompaniesResponse != null)
{
List<com.dopay.onboarding.data.bean.Company> companies = fetchCompaniesResponse.getCompanies();
if (companies != null && !companies.isEmpty()) {
showCompaniesDialog(companies);
}
Toast.makeText(getContext(), "companies response is not null", Toast.LENGTH_LONG).show();
}
}).subscribe();
Your suggestions are very helpful
Thanks
R
Check first the docs of the flatMapoperator, it says:
transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable.
How can I call the same function with different params?
You can extract the method getCompaniesData and pass to it two integers (or a proper class), in this way:
Observable<T> getCompaniesData(Param param);
How can I combine the different results?
You can apply the operator flatMap to the multiple Observables emitted by the defined method.
Observable<T> foo(Param... params) {
return Observable.fromArray(params)
.flatMap(this::getCompaniesData)
...
}
Related
I have a two queries which return two long values. I am setting these two long values to be displayed in individual text views. Finally I have a third text view which displays the combined value of both longs. I am having a problem getting the combined total to show as its setting the value before the livedata is returned.
Below is a snippet of the code
private void getData() {
mViewModelReframing.totalWorkouts(pkUserId).observe(getViewLifecycleOwner(), new Observer<List<ModelStatsTotalWorkouts>>() {
#Override
public void onChanged(List<ModelStatsTotalWorkouts> modelStatsTotalWorkouts) {
for (ModelStatsTotalWorkouts list : modelStatsTotalWorkouts) {
totalReframeWorkouts = list.getTotalWorkouts();
}
if (totalReframeWorkouts == 0) {
tvTotalReframes.setText(0 + getString(R.string.workouts_empty));
} else {
tvTotalReframes.setText("" + totalReframeWorkouts);
}
}
});
mViewModelCheckIn.totalWorkouts(pkUserId).observe(getViewLifecycleOwner(), new Observer<List<ModelStatsTotalWorkouts>>() {
#Override
public void onChanged(List<ModelStatsTotalWorkouts> tableCheckIns) {
for (ModelStatsTotalWorkouts list : tableCheckIns) {
totalCheckInWorkouts = list.getTotalWorkouts();
}
tvTotalCheckIns.setText("" + totalCheckInWorkouts);
// Combine both longs together for a combined total.
totalWorkouts = totalReframeWorkouts + totalCheckInWorkouts;
tvTotalWorkouts.setText("" + totalWorkouts);
}
});
}
Is there a better way to write the logic to achieve the desired result without the issue of the livedata not being returned fast enough?
Whenever you use independent Reactive streams like this (LiveData, RxJava, etc) you are going to have race conditions. You need to make explicit the dependencies for an action to happen - in this case your ability to update the UI in the way that you want had dependencies on BOTH APIs returning. This is the RxJava equivalent of zip. A few tips:
Consider using only a single Viewmodel for a view. The viewmodel should really be preparing data for your view specificially. In this case, it should really be that singular ViewModel that handles combining this data before passing it to your vew at all.
Barring that, since you've chosen LiveData here, you can do what you want by using a MediatorLiveData. Essentially, it acts as a composite stream source that depends on whichever other LiveData streams you add to it as described by that article. In this way, you can explicitly wait for all the needed values to arrive before you try to update the UI.
I solved the question by using this method:
public LiveData<List<ModelStatsTotalWorkouts>> totalWorkoutsCombined(long userId) {
LiveData liveData1 = database.getUsersDao().totalReframeWorkouts(userId);
LiveData liveData2 = database.getUsersDao().totalCheckInWorkouts(userId);
MediatorLiveData liveDataMerger = new MediatorLiveData<>();
liveDataMerger.addSource(liveData1, value -> liveDataMerger.setValue(value));
liveDataMerger.addSource(liveData2, value -> liveDataMerger.setValue(value));
return liveDataMerger;
}
Code
Author author = baseRealm.where(Author.class).equalTo("id", mId).findFirst();
public boolean checkGlobalSyncStatus(Author author, List<Books> mBooks) {
final boolean[] isJobSynchronized = {false};
Observable.fromIterable(mBooks)
.filter(Books::isChanged)
.doOnNext(book -> isJobSynchronized[0] = true)
.just(author)
.flatMapIterable(Author::getAllBooks)
.filter(MyBook::isChanged)
.doOnNext(mBook -> isJobSynchronized[0] = true)
.just(author)
.flatMapIterable(Author::getAllWriters)
.filter(Writers::isChanged)
.doOnNext(jobPage -> isJobSynchronized[0] = true)
.subscribe();
return isJobSynchronized[0];
}
Problem
fromIterable(mBooks) is called from static-reference Observable BUT just(author) is called from instance-reference.
I only want to get this operation done in single query. I can make different observable for each and perform desired operation but that would be lengthy.
Why?
By doing so, SonarQube is giving me unsuccessful check and forcing me to remove instance-reference.
Any alternatives will be appreciated.
You are trying to use just() as an operator when it is really an observable. It looks like your intention is to use the passed in author to make a series of queries, and then check that any of the books associated with the author have "changed".
Additionally, you are trying to return a boolean value that likely has not been set by the time the return occurs. You may need to block and wait for the observer chain to finish if you want the value. More likely, you want the observer chain to finish if any book has changed.
Additionally, the series of steps where you set the flag to true come down to setting the flag to true the first time.
Instead of just(), use map() to rebind the original author into the observer chain. Use the toBlocking() operator to make the process synchronous.
Observable.fromIterable(mBooks)
.filter(Books::isChanged)
.toBlocking()
.subscribe( ignored -> isJobSynchronized[0] = true );
return isJobSynchronized[0];
Since the (presumably) asynchronous queries are no longer necessary to compute the value, remove RxJava:
return mBooks.stream()
.filter(Books::isChanged)
.anyMatch();
There is no reason to use RxJava here, however, the proper combination of operators would be as follows:
Author author = baseRealm.where(Author.class).equalTo("id", mId).findFirst();
public boolean checkGlobalSyncStatus(Author author, List<Books> mBooks) {
return Single.concat(
Observable.fromIterable(mBooks)
.any(Books::isChanged)
, // ---------------------------------------------
Observable.fromIterable(author.getAllBooks)
.any(MyBook::isChanged)
, // ---------------------------------------------
Observable.fromIterable(author.getAllWriters)
.any(Writers::isChanged)
)
.any(bool -> bool)
.blockingGet();
}
I'm trying to execute two Maybe at once and call a specific method once both are done. This works if both Observables return a value but in certain cases one might not emit an item thus calling only doOnComplete and not doOnSuccess. So if one of those Maybes' doesn't call doOnSuccess the zip() block isn't executed. I'm wondering how to handle such a scenario?
Following my code (stripped down to the essential part):
private void fetchData(){
Maybe<Integer> maybeOne = getId(); // may return Maybe.empty()
Maybe<List<String>> maybeTwo = getList();
Maybe.zip(maybeOne, maybeTwo, (id, list) -> {
initView(id, list); // only called if values have been emitted
return true;
}).subscribe();
}
I would expect that the zip() block is always called but with null values in case the Maybe didn't call onSuccess. This isn't the case so can I handle such a scenario?
You can use the materialize operator. It basically transforms the serial invocation into object (wrapped inside a Notification object).
Observable.zip(maybeOne.toObservable().materialize(),
maybeTwo.toObservable().materialize(), (id, list) -> {
Log.d(TAG, "zip completed");
return true;
}).subscribe();
Now your zip will always "finish", but your real data can be retrieved using:
id.getValue()
And if your maybe is Maybe.empty() then it will not return the value, but null.
I have one async-method, like this:
void getPlaceInfo(Place place, PlaceCallback callback)
For example my PlaceCallback has one method:
void success(InfoPlace place);
I want create Observable for waiting response from two requests:
getPlaceInfo(...); // first call
getPlaceInfo(...); // second call
And then I want to get both response at the same time.
Can I make it?
So you need to combine 2 callbacks to evaluate a function like:
Response computeResponse(InfoPlace infoPlace, InfoPlace infoPlace2) {
...
}
And you want to use Rx for this. There is two problem here; wrapping the callback method into Observable, and combine them.
You can't just instantiate an Observable and call myObservable.send(value) or myObservable.next(value). The first solution is to use a Subject. Another solution (the one below) is to create the observable with Observable.create(). You call the callback method and create the listener inside the method Observable.create(), because it's inside Observable.create() that you can call onSuccess() method, the method who told the Observable to pass down a value:
Single<InfoPlace> getInfoPlaceSingle(Place place) {
return Single.create(e -> getPlaceInfo(place, new PlaceCallback() {
#Override
public void success(InfoPlace infoPlace) {
e.onSuccess(infoPlace);
}
}));
}
Or with Java 8:
Single<InfoPlace> getInfoPlaceSingle(Place place) {
return Single.create(e -> getPlaceInfo(place, e::onSuccess));
}
Note, I used Single over Observable, since you await only one value. The interest is a more expressive contract for your function.
Wrapping things into Observable is a pain, but now you are in the reactive realm where Rx provide you a complete toolbox to deal with them. The second part - combining the observables - is easy:
Single.zip(getInfoPlaceSingle(place1), getInfoPlaceSingle(place2), this::computeResponse);
Wrap your async calls using Observable.fromEmitter() and you can then use Observable.zip() to combine the calls.
I'm new to RXJava and i'm having trouble understanding how to chain together the result of API calls.
I'm making two API calls using retrofit, A and B, which both return an observable List of objects. Both API calls are independent so I want to make both at the same time, but to achieve my final result, I need to first take the result of A, do some work, then combine that with the result of B to populate my list adapter.
Make API Call A
Make API Call B
Take A's result and create result X
Take Result of B + X and populate adapter
#GET("/{object_id}/object_a")
Observable<List<Object_A>> getObjectAList(
#Path("object_id") long object_id);
#GET("/{object_id}/object_b")
Observable<List<Object_B>> getObjectBList(
#Path("object_id") long object_id);
This is where I get lost trying to use RX java. I can take the result of api call A and do my work
but I'm not sure how to take the result I just generated and combine it with API Call B.
aService. getObjectAList(object_a.getID())
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.main)
.subscribe(new Action1<List<Object_A>>() {
#Override
public void call(List<Section> sections) {
// Do Stuff Here...
// Now i need to take this result and combine it with API Call B...
}
});
I want to make both API calls at the same time, but i'm not sure how to chain together and combine API calls. Any help is appreciative.
Something like this?
Observable
// make api call for A list and B list
.combineLatest(getObjectAList(), getObjectBList(), new Func2<List<Object_A>, List<Object_B>, Object>() {
#Override
public Object call(List<Object_A> o, List<Object_B> o2) {
// Do what you need to do in the background thread
List x = createX(o);
List y = createY(x, o2);
return y;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Object>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(Object y) {
// UI thread, do what you need e.g. renders the list
mAdapter.setList(y);
}
});
Taking care of replacing the proper types should bring you quite close to the solution.
The question is : how would you combine results ?
Building a new result from List and List ? Combine A objects with B objects ?
Answer to this question help to find the right operator for your problem.
A simple example of combining results can be this :
getObjectAList().zipWith(getObjectBList(), (aList, bList) -> // combine both list to build a new result)).subscribe()
You can combine elements of the list too with another operator (combineLatest for example)
aObs = getObjectAList().flatMap(Observable::from);
bObs = getObjectBList().flatMap(Observable::from);
Observable.combineLatest(aObs, bObs, (a,b) -> // combine a object with b object).subscribe();
For all of this examples above, requests will be done in parallel by retrofit.
I'd probably do something like the following
Observable convertedObservable = getObjectAList
.map(object_as -> convertAToX(object_as));
Observable.combineLatest(convertedObservable, getObjectBList, (listx, listb) -> {
return listx.addAll(listb);
}).subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.main)
.subscribe(r -> {
setAdapterWith(r);
});
Keep in mind this is using lambdas instead of anonymous classes but you should get the gist. Map is a great way of converting one object type to another (results of A to Results of X). So you can decide how convertAToX method works for you. Then you can use combineLastest on the converted A-X and B to return the list of R which updates your adapter
Ideally this is all in a ViewModel of some kind where getObjectAList and getObjectBList can me mocked on with Mock observables and you can test all the logic easily :)