Consider the following set and object:
Observable.from(users);
What I would like to do is location.userId.equals(user.userId);
over the list of users, and after the first meeting with the request location.userId.equals(user.userId);
return the merged object in the database. If userId
does not match the transition to the next user. And end the loop as soon as 1 match is found.
How can I achieve this with RxJava?
Originally intended to use:
Observable.zip(Observable.from(users), Observable.just(location), new Func2<User, Location, UserLocation>() { ... });`
Does anyone have a better alternative?
Edit:
I thought that maybe I can solve it with a simple solution, but well, I will explain it more clearly.
So, as soon as I have location.userId
and user.userId
, I also need to query the database, which will return an Observable<Boolean>
value indicating whether it is also true in our database. If this condition matches, I return a combined object.
So, the whole stream looks like this:
for each user in Users { checkIfAlreadyExistsInDatabase(user.userId, location.userId) // Returns Observable<Boolean> // If exists in db AND user.userId == location.userId return combined object and terminate the loop }
This previously ran synchronously without RxJava
. I converted the checkIfAlreadyExistsInDatabase
method to Rx and used Schedulers.io
to ping the database in the background thread to make the application more responsive. The problem arose when I had to iterate over an array of users and map the identifier to the "Location" field, as well as "ping my database".
To call the checkIfAlreadyExistsInDatabase
method, I need to capture user.userId
, and for this I need to iterate over users
and filter using location.userId
.
So:
- Iterate over users
- If user.userId matches location.userId, check if the database exists
- If a merged object exists in the database
- End the loop if 1 match is found