Paging library - populate from cache while requesting from network - android

I have to load cached version of data from database and simultaneously I want to make a request to server for fresh data and I want to do this on per page basis.
So, for example for first page I want to show a cached version of first page data from database while requesting fresh data only for first page.
I want to achieve this using Paging Library.
I tried creating custom data source which helped me intercept page load request which then I used to make a network call with required page number and limit and meanwhile I returned a cached version from db, the problem is after getting fresh data from network I update the database but those updates are not reflected.
(I believe the whole table is being observed for any modifications using Invalidation Tracker and data source is invalidated whenever tables are invalidated, I added that tracker in my data source too but still it ain't working; I was able to make out that Invalidation Tracker thing by temporarily creating: LivePagedListProvider getJobs() in JobDao and checking generated implementation)
Code:
public class JobListDataSource<T> extends TiledDataSource<T> {
private final JobsRepository mJobsRepository;
private final InvalidationTracker.Observer mObserver;
String query = "";
public JobListDataSource(JobsRepository jobsRepository) {
mJobsRepository = jobsRepository;
mObserver = new InvalidationTracker.Observer(JobEntity.TABLE_NAME) {
#Override
public void onInvalidated(#NonNull Set<String> tables) {
invalidate();
}
};
jobsRepository.addInvalidationTracker(mObserver);
}
#Override
public int countItems() {
return DataSource.COUNT_UNDEFINED;
}
#Override
public List<T> loadRange(int startPosition, int count) {
return (List<T>) mJobsRepository.getJobs(query, startPosition, count);
}
public void setQuery(String query) {
this.query = query;
}
}
Jobs repository functions:
public List<JobEntity> getJobs(String query, int startPosition, int count) {
if (!isJobListInit) {
JobList jobList = mApiService.getOpenJobList(
mRequestJobList.setPageNo(startPosition/count + 1)
.setMaxResults(count)
.setSearchKeyword(query)
).blockingSingle();
mJobDao.insert(jobList.getJobsData());
}
return mJobDao.getJobs(startPosition, count);
}
public void addInvalidationTracker(InvalidationTracker.Observer observer) {
mAppDatabase.getInvalidationTracker().addObserver(observer);
}

So I understood why it wasn't working, there was a mistake at my end, I was passing wrong parameters to getJobs method of JobDao in JobsRepository.
The getJobs method of JobDao goes as follows:
#Query("SELECT * FROM jobs ORDER BY jobID ASC LIMIT :limit OFFSET :offset")
List<JobEntity> getJobs(int limit, int offset);
And the call getJobs() in JobsRepository goes as follows:
return mJobDao.getJobs(startPosition, count);
So the first parameter was the limit and the next one was the offset but I was passing other way around.
Now it works like a charm!
Furthermore, I made a change to getJobs() in JobsRepository:
First get data from db, if available return and make an async request to network if required.
If no data is available in db, make a synchronous call to network, get data from network, parse it and save it db and now access latest data from db and return it.
So the function goes like this:
//you can even refactor this code so that all the network related stuff is in one class and just call that method
public List<JobListItemEntity> getJobs(String query, int startPosition, int count) {
Observable<JobList> jobListObservable = mApiService.getOpenJobList(
mRequestJobList.setPageNo(startPosition / count + 1)
.setMaxResults(count)
.setSearchKeyword(query));
List<JobListItemEntity> jobs = mJobDao.getJobsLimitOffset(count, startPosition);
//no data in db, make a synchronous call to network to get the data
if (jobs.size() == 0) {
JobList jobList = jobListObservable.blockingSingle();
updateJobList(jobList, startPosition, false);
} else if (shouldFetchJobList(jobs)) {
//data available in db, so show a cached version and make async network call to update data as this data is no longer fresh
jobListObservable.subscribe(new Observer<JobList>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(JobList jobList) {
updateJobList(jobList, startPosition, true);
}
#Override
public void onError(Throwable e) {
Timber.e(e);
}
#Override
public void onComplete() {
}
});
}
return mJobDao.getJobsLimitOffset(count, startPosition);
}
updateJobList() code:
private void updateJobList(JobList jobList, int startPosition, boolean performInvalidation) {
JobListItemEntity[] jobs = jobList.getJobsData();
Date currentDate = Calendar.getInstance().getTime();
//tracks when this item was inserted in db, used in calculating whether data is stale
for (int i = 0; i < jobs.length; i++) {
jobs[i].insertedAt = currentDate;
}
mJobDao.insert(jobs);
if (performInvalidation) {
mJobListDataSource.invalidate();
}
}
(I also renamed the getJobs() in JobDao to getJobsLimitOffset() as it makes it more readable and that is also the way methods are generated by paging library)

Related

How to Implement Paginate a query on android Firestore [duplicate]

This question already has answers here:
How to paginate Firestore with Android?
(3 answers)
Closed 4 years ago.
On working around to learn firebase firestore for an example from GitHub friendly eat app
I thought to implement pagination to limiting nodes for 10
private static final int LIMIT = 10;
in the firestore example app the mAdapter loads data/nodes as below
mFirestore = FirebaseFirestore.getInstance();
// Get ${LIMIT} restaurants
mQuery = mFirestore.collection("restaurants")
.orderBy("avgRating", Query.Direction.DESCENDING)
.limit(LIMIT);
// RecyclerView
mAdapter = new RestaurantAdapter(mQuery, this) {
#Override
protected void onDataChanged() {
// Show/hide content if the query returns empty.
if (getItemCount() == 0) {
mRestaurantsRecycler.setVisibility(View.GONE);
mEmptyView.setVisibility(View.VISIBLE);
} else {
mRestaurantsRecycler.setVisibility(View.VISIBLE);
mEmptyView.setVisibility(View.GONE);
}
}
#Override
protected void onError(FirebaseFirestoreException e) {
// Show a snackbar on errors
Snackbar.make(findViewById(android.R.id.content),
"Error: check logs for info.", Snackbar.LENGTH_LONG).show();
}
};
mRestaurantsRecycler.setLayoutManager(new LinearLayoutManager(this));
mRestaurantsRecycler.setAdapter(mAdapter);
// Filter Dialog
mFilterDialog = new FilterDialogFragment();
}
and
#Override
public void onStart() {
super.onStart();
// Start sign in if necessary
if (shouldStartSignIn()) {
startSignIn();
return;
}
// Apply filters
onFilter(mViewModel.getFilters());
// Start listening for Firestore updates
if (mAdapter != null) {
mAdapter.startListening();
}
}
on firestore docs says about to paginate
// Construct query for first 25 cities, ordered by population
Query first = db.collection("cities")
.orderBy("population")
.limit(25);
first.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot documentSnapshots) {
// ...
// Get the last visible document
DocumentSnapshot lastVisible =
documentSnapshots.getDocuments()
.get(documentSnapshots.size() -1);
// Construct a new query starting at this document,
// get the next 25 cities.
Query next = db.collection("cities")
.orderBy("population")
.startAfter(lastVisible)
.limit(25);
// Use the query for pagination
// ...
}
});
combining those above codes how should I implement paginate to load more than 10
nodes to load when I scroll to the bottom of the recycler view
// Use the query for pagination
// ...
Update: I am working based on firestore doc about Paginate query and taking look at a possible duplicate of another question I did not get it to done working
Thank you
Here I found solution around but not better one, if is there any better way please post
by saving RecyclerView state before loading more nodes and reloading RecyclerView state after increasing the limit
private static final int LIMIT = 10;
Changed to
private int LIMIT = 10;
when recyclerView is scrolled to the bottom
mRestaurantsRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
final int mLastVisibleItemPosition = mManager.findLastVisibleItemPosition();
if ( mLastVisibleItemPosition == (LIMIT-1)) {
LIMIT = LIMIT*2;
showSpotDialog();
// save RecyclerView state
mBundleRecyclerViewState = new Bundle();
Parcelable listState = mRestaurantsRecycler.getLayoutManager().onSaveInstanceState();
mBundleRecyclerViewState.putParcelable(KEY_RECYCLER_STATE, listState);
loadMore(query);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// restore RecyclerView state
if (mBundleRecyclerViewState != null) {
Parcelable listState = mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE);
mRestaurantsRecycler.getLayoutManager().onRestoreInstanceState(listState);
}
hideSpotDialog();
}
}, 500);
}
}
});
it looks very unusual when nodes are loaded after the limit, but no way for now...
and yes I am looking for loading more nodes without flaws
I have extended the FirestoreAdapter as followed:
Keep track DocumentSnapshots identifier.
private Set<String> mIdentifier = new HashSet<>();
Add a new public method for pagination, as i expect the rest of the query to remain the same, the given query does not need to be changed
/**
* Extends the query to load even more data rows. This method will do nothing if the query has
* not yet been set.
* #param limit the new limit
*/
public void paginate(long limit) {
if (mQuery != null) {
if (mRegistration != null) {
mRegistration.remove();
mRegistration = null;
}
// Expect the query to stay the same, only the limit will change
mQuery = mQuery.limit(limit);
startListening();
}
}
Clear the identifier in the setQuery(Query) method by calling mIdentifier.clear()
Adopt the onDocumentAdded(DocumentChange) and the onDocumentRemoved(DocumentChange) as followed
protected void onDocumentAdded(DocumentChange change) {
if (!mIdentifier.contains(change.getDocument().getId())) {
mSnapshots.add(change.getNewIndex(), change.getDocument());
mIdentifier.add(change.getDocument().getId());
notifyItemInserted(change.getNewIndex());
}
}
protected void onDocumentRemoved(DocumentChange change) {
mSnapshots.remove(change.getOldIndex());
mIdentifier.remove(change.getDocument().getId());
notifyItemRemoved(change.getOldIndex());
}
For the onScrolling listener i stick to this guide: Endless Scrolling with AdapterViews and RecyclerView

How to handel no results with Android Room and RxJava 2?

I have database with table contact and I want to check if there is contact with some phone number.
#Query("SELECT * FROM contact WHERE phone_number = :number")
Flowable<Contact> findByPhoneNumber(int number);
I have RxJava 2 Composite disposable with statement from above to check if there is contact with phone number.
disposable.add(Db.with(context).getContactsDao().findByPhoneNumber(phoneNumber)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSubscriber<Contact>() {
#Override
public void onNext(final Contact contact) {
Log.d("TAG", "phone number fined");
Conversation conversation;
if(contact != null){
conversation = Db.with(context).getConversationsDao().findBySender(contact.getContactId());
if(conversation != null){
conversation.setUpdatedAt(Utils.getDateAndTimeNow());
saveConversation(contact, conversation, context, text, phoneNumber, false);
} else {
conversation = getConversation(contact, contact.getPhoneNumber());
saveConversation(contact, conversation, context, text, phoneNumber, true);
}
} else {
conversation = Db.with(context).getConversationsDao().findByPhone(phoneNumber);
if(conversation != null){
conversation.setUpdatedAt(Utils.getDateAndTimeNow());
saveConversation(contact, conversation, context, text, phoneNumber, false);
} else {
conversation = getConversation(contact, phoneNumber);
saveConversation(contact, conversation, context, text, phoneNumber, true);
}
}
}
#Override
public void onError(Throwable t) {
Log.d("TAG", "find phone number throwable");
Toast.makeText(context, t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
#Override
public void onComplete() {
Log.d("TAG", "onComplete");
}
}));
This is working fine if query can find contact with required phone number, but if there is result, it nothing happens.
Here are two test cases that I wrote and they work fine:
#RunWith(AndroidJUnit4.class)
public class ContactsTest {
private AppDatabase db;
#Rule
public InstantTaskExecutorRule instantTaskExecutorRule =
new InstantTaskExecutorRule();
#Before
public void initDb() throws Exception {
db = Room.inMemoryDatabaseBuilder(
InstrumentationRegistry.getContext(),
AppDatabase.class)
// allowing main thread queries, just for testing
.allowMainThreadQueries()
.build();
}
#After
public void close(){
db.close();
}
#Test
public void insertAndFindTest(){
final Contact contact = new Contact();
contact.setName("Test");
contact.setPhoneNumber(555);
db.contactsDao()
.insert(contact);
db.contactsDao().findByPhoneNumber(contact.getPhoneNumber())
.test()
.assertValue(new Predicate<Contact>() {
#Override
public boolean test(#NonNull Contact savedContact) throws Exception {
if(savedContact.getPhoneNumber() == contact.getPhoneNumber()){
return true;
}
return false;
}
});
}
#Test
public void findNoValues(){
db.contactsDao().findByPhoneNumber(333)
.test()
.assertNoValues();
}
}
How I can solve this?
As said here, you can use Maybe or Single for this case:
Maybe
#Query("SELECT * FROM Users WHERE id = :userId")
Maybe<User> getUserById(String userId);
Here’s what happens:
When there is no user in the database and the query returns no rows, Maybe will complete.
When there is a user in the database, Maybe will trigger onSuccess and it will complete.
If the user is updated after Maybe was completed, nothing happens.
Single
#Query("SELECT * FROM Users WHERE id = :userId")
Single<User> getUserById(String userId);
Here are some scenarios:
When there is no user in the database and the query returns no rows, Single will trigger onError(EmptyResultSetException.class)
When there is a user in the database, Single will trigger onSuccess.
If the user is updated after Single.onComplete was called, nothing happens, since the stream was completed.
It was added in version 1.0.0-alpha5.
If you want to use your entity only once, Single or Maybe is sufficient. But if you want to observe if your query is updated you can use Flowable and wrap your object in List, so when there is no results you will get empty list, and when after that, database is updated you will get another event with your result in list.
Code
#Query("SELECT * FROM contact WHERE phone_number = :number LIMIT 1")
Flowable<List<Contact>> findByPhoneNumber(int number)
I believe it's usefull in some scenarios. The drawback is that you have to access object like resultList.get(0)
When you use Flowable (and LiveData too) as a return value in your Dao class then your query never stops emitting data as Room is monitoring tables for data changes. Quoting official documentation:
Furthermore, if the response is an observable data type, such as
Flowable or LiveData, Room watches all tables referenced in the query
for invalidation.
Not sure what's the best way of handling such situation but what worked for me was a good old .timeout() operator. Please have a look at the following test and follow comments:
#Test
public void shouldCompleteIfForced() throws InterruptedException {
// given
TestScheduler testScheduler = new TestScheduler();
// when asking db for non existent project
TestSubscriber<Project> test = projectDao.getProject("non existent project")
.timeout(4, TimeUnit.SECONDS, testScheduler)
.test();
// then hang forever waiting for first emission which will never happen
// as there is no such project
test.assertNoValues();
test.assertNotComplete();
test.assertNoErrors();
// when time passes and we trigger timeout() operator
testScheduler.advanceTimeBy(10, TimeUnit.SECONDS);
// then finally break stream with TimeoutException error ...
test.assertError(TimeoutException.class);
}
I guess you also could use wrapper with Single. Like:
public class QueryResult<D> {
public D data;
public QueryResult() {}
public QueryResult(D data) {
this.data = data;
}
public boolean isEmpty(){
return data != null;
}
}
And use it like:
public Single<QueryResult<Transaction>> getTransaction(long id) {
return createSingle(() -> database.getTransactionDao().getTransaction(id))
.map(QueryResult::new);
}
Where createAsyncSingle:
protected <T> Single<T> createSingle(final Callable<T> func) {
return Single.create(emitter -> {
try {
T result = func.call();
emitter.onSuccess(result);
} catch (Exception ex) {
Log.e("TAG", "Error of operation with db");
}
});
}
Don't forget to use IO thread.
#Query("SELECT * FROM contact WHERE phone_number = :number")
Flowable<List<Contact>> findByPhoneNumber(int number);
then
Optional<Contact> queryPhone(int number) {
findByPhoneNumber(number).map { list ->
if (list.isEmpt()) return Optional.empty() else return Optional.of(list[0])
}
}
as described here
I find it's strange that this behaviour is no where to be found on official docs though

Android: infinite scroll with rx-java using repeatWhen, takeUntil and filter with retrofit

I am using Retrofit 2.2 with RxJava.
The pagination works like this: I get the first batch of data, I have to request the second batch of data with the same params except one which is the lastUpdated date and then if I get empty or the same batch of data it means there are no more items. I have found this great article https://medium.com/#v.danylo/server-polling-and-retrying-failed-operations-with-retrofit-and-rxjava-8bcc7e641a5a#.40aeibaja on how to do it. So my code is:
private Observable<Integer> syncDataPoints(final String baseUrl, final String apiKey,
final long surveyGroupId) {
final List<ApiDataPoint> lastBatch = new ArrayList<>();
Timber.d("start syncDataPoints");
return loadAndSave(baseUrl, apiKey, surveyGroupId, lastBatch)
.repeatWhen(new Func1<Observable<? extends Void>, Observable<?>>() {
#Override
public Observable<?> call(final Observable<? extends Void> observable) {
Timber.d("Calling repeatWhen");
return observable.delay(5, TimeUnit.SECONDS);
}
})
.takeUntil(new Func1<List<ApiDataPoint>, Boolean>() {
#Override
public Boolean call(List<ApiDataPoint> apiDataPoints) {
boolean done = apiDataPoints.isEmpty();
if (done) {
Timber.d("takeUntil : finished");
} else {
Timber.d("takeUntil : will query again");
}
return done;
}
})
.filter(new Func1<List<ApiDataPoint>, Boolean>() {
#Override
public Boolean call(List<ApiDataPoint> apiDataPoints) {
boolean unfiltered = apiDataPoints.isEmpty();
if (unfiltered) {
Timber.d("filtered");
} else {
Timber.d("not filtered");
}
return unfiltered;
}
}).map(new Func1<List<ApiDataPoint>, Integer>() {
#Override
public Integer call(List<ApiDataPoint> apiDataPoints) {
Timber.d("Finished polling server");
return 0;
}
});
}
private Observable<List<ApiDataPoint>> loadAndSave(final String baseUrl, final String apiKey,
final long surveyGroupId, final List<ApiDataPoint> lastBatch) {
return loadNewDataPoints(baseUrl, apiKey, surveyGroupId)
.concatMap(new Func1<ApiLocaleResult, Observable<List<ApiDataPoint>>>() {
#Override
public Observable<List<ApiDataPoint>> call(ApiLocaleResult apiLocaleResult) {
return saveToDataBase(apiLocaleResult, lastBatch);
}
});
}
private Observable<ApiLocaleResult> loadNewDataPoints(final String baseUrl, final String apiKey,
final long surveyGroupId) {
Timber.d("loadNewDataPoints");
return Observable.just(true).concatMap(new Func1<Object, Observable<ApiLocaleResult>>() {
#Override
public Observable<ApiLocaleResult> call(Object o) {
Timber.d("loadNewDataPoints call");
return restApi
.loadNewDataPoints(baseUrl, apiKey, surveyGroupId,
getSyncedTime(surveyGroupId));
}
});
}
As you can see the interesting method is loadNewDataPoints and I want it to be called until there are no more datapoints. As you can see Observable.just(true).concatMap is a hack because if I remove this concat map the restApi.loadNewDataPoints(....) does not get called although in the logs I can see that the api does get called but with the same old params and of course it returns the same results as the first time so syncing stops, saveToDataBase does get called fine. With my hack it works but I want to understand why it does not work the other way and also if there is a better way to do this. Thanks a lot!
So, I've written this kind of APIs (it's called Keyset Pagination) and implemented Rx clients against them.
This is one of the cases where BehaviorSubjects are useful:
S initialState = null;
BehaviorProcessor<T> subject = BehaviorProcessor.createDefault(initialState);
return subject
.flatMap(state -> getNextElements(state).singleOrError().toFlowable(), Pair::of, 1)
.serialize()
.flatMap(stateValuePair -> {
S state = stateValuePair.getLeft();
R retrievedValue = stateValuePair.getRight();
if(isEmpty(retrievedValue)) {
subject.onComplete();
return Flowable.empty();
} else {
subject.onNext(getNextState(state, retrievedValue));
return Flowable.just(retrievedValue);
}
}
.doOnUnsubscribe(subject::onCompleted)
.map(value -> ...)
Here
getNextElement performs the network call based on a state and returns a reactive stream with a single value
isEmpty determines whether the returned value is empty indicating end of elements
getNextState combines the passed-in state with the retrieved value to determine the next state for getNextElement.
It will work correctly if an error occurs (it will be propagated) and if you unsubscribe before the end (queries will get terminated).
Of course, in your specific case these don't need to be separate methods or complex types.

Long running RxJava Subscriptions with refreshable data

I'm looking to set up a long running data subscription to a particular data object in Android/RxJava. Specifically a combination of a Retrofit REST call paired with cached data. I've done this pretty simply just wrapping an API call with data, were the API call is Retrofit returning an Observable:
class OpenWeather {
...
Observable<CurrentWeather> OpenWeather.getLocalWeather()
...
}
The simple implementation would be:
public static Observable<CurrentWeather> getWeatherOnce() {
if (currentWeather != null)
return Observable.just(currentWeather);
return OpenWeather.getLocalWeather()
.map(weather -> currentWeather = weather);
}
private static CurrentWeather currentWeather;
The problem is that there is no way to notify when the "current weather" has been updated. The simplest way to add refreshable data with long running updates between subscriptions would be to use a BehaviorSubject like such:
public class DataModel {
public enum DataState {
ANY, // whatever is available, don't require absolute newest
LATEST, // needs to be the latest and anything new
}
private final static BehaviorSubject<CurrentWeather> currentWeatherSubject = BehaviorSubject.create();
public static Observable<CurrentWeather> getCurrentWeather(DataState state) {
synchronized (currentWeatherSubject) {
if (state == DataState.LATEST || currentWeatherSubject.getValue() == null) {
OpenWeather.getLocalWeather()
.subscribeOn(Schedulers.io())
.toSingle()
.subscribe(new SingleSubscriber<CurrentWeather>() {
#Override
public void onSuccess(CurrentWeather currentWeather) {
currentWeatherSubject.onNext(currentWeather);
}
#Override
public void onError(Throwable error) {
// ?? currentWeatherSubject.onError(error);
}
});
}
}
return currentWeatherSubject.asObservable();
}
}
Using the BehaviorSubject, when getting the current weather, get either the last cached entry and any updates as they occur. Thoughts?
So I'm sure I'm doing something wrong here as there seems there should be an easier way or more elegant way.

RxAnadroid concatenating streams

I'm making a simple weather app to learn RxAndroid and I'm faced with the following issue.
I first load cities I'm interested in and then ask for the weather of each one of them.
getCitiesUseCase returns an Observable<List<City>> that I load from the data base. I send that list of cities to my view to display them and then ask for the weather individually (flatmap) inside the subscriber.
Subscription subscription = getCitiesUseCase.execute().flatMap(new Func1<List<City>, Observable<City>>() {
#Override
public Observable<City> call(List<City> cities) {
citiesView.addCities(cities);
return Observable.from(cities);
}
}).subscribe(new Subscriber<City>() {
#Override
public void onCompleted() {
subscriptions.remove(this);
this.unsubscribe();
}
#Override
public void onError(Throwable e) {
Log.e(this.getClass().getSimpleName(), e.toString());
}
#Override
public void onNext(City city) {
getCityWeatherUseCase.setLatLon(city.getLat().toString(), city.getLon().toString(), city.getId());
getCityWeather(city);
}
});
subscriptions.add(subscription);
Now the getCityWeather() method looks like this:
private void getCityWeather(final City city) {
subscriptions.add(getCityWeatherUseCase.execute().subscribe(new Subscriber<CityWeather>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
Log.e("error", e.toString());
}
#Override
public void onNext(CityWeather cityWeather) {
city.setCityWeather(cityWeather);
citiesView.updateCity(city);
}
}));
}
Everything works fine and as expected, but the fact that I'm subscribing to an observer inside a subcriber doesnt feel right. I know rxJava lets you play around with subscribers to prevent this kind of things but I really dont know how to improve my code further. Keep in mind that I need a city in order to ask for its weather.
Merry chrismas!
One approach could be the following. (I'm using retrolambda - so wherever you see ->, just replace with a new anonymous inner class).
Note that I'm using flatMap to spin up the weather data requests, rather than Observable.concat like your question suggests. The reason for this is that your scheduler (e.g. io()) will handle these in parallel and send the results through when they are available. However, with Observable.concat, these requests would be serialized so they'd be forced to happen one at a time - nullifying the benefits of a thread pool like io().
private class City {
public String name;
public City(String name) {
this.name = name;
}
public void setWeather(Weather weather) { /*...*/ }
}
private class Weather {
public String status;
public Weather(String status) {
this.status = status;
}
}
private Observable<Weather> getWeather(City city) {
// call your weather API here..
return Observable.just(new Weather("Sunny"));
}
#Test
public void test() {
Observable<List<City>> citiesObs = Observable.create(new Observable.OnSubscribe<List<City>>() {
#Override
public void call(Subscriber<? super List<City>> subscriber) {
// do work
final List<City> cities = new ArrayList<>();
cities.add(new City("Paris"));
cities.add(new City("Tokyo"));
cities.add(new City("Oslo"));
// send results
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(cities);
subscriber.onCompleted();
}
}
});
Observable<City> obs = citiesObs
// inject a side effect
.doOnNext(list -> {
// pass `list` to your view here
})
// turn Observable<Iterable<T>> into Observable<T>
.flatMapIterable(list -> list)
// Map a city to an observable that fetches Weather data
// Your scheduler can take care of these at once.
.flatMap(city -> {
return getWeather(city)
// another side effect
.doOnNext(weather -> {
city.setWeather(weather);
})
// map baack to city, just for the heck of it
.map($ -> city);
});
TestSubscriber sub = TestSubscriber.create();
obs.subscribe(sub);
sub.awaitTerminalEvent();
sub.assertValueCount(3);
}
Also note that in order to take advantage of io(), you'd need to add a call to subscribeOn(Schedulers.io()) to tell the observable to begin doing work on the io thread pool. When you want to pass control to another thread, for example your view, you could insert a observeOn(AndroidSchedulers.mainThread()) before your side-effect (or mapping). If you want to bounce control back to the background thread(s) for your weather calls, you could then add another call to observeOn(Schedulers.io()) right before you flatMap to getWeather(City).

Categories

Resources