Skip to content

Instantly share code, notes, and snippets.

@tranngoclam
Created September 23, 2016 06:42
Show Gist options
  • Save tranngoclam/58920ecc59a0427b7b6625a234abd26b to your computer and use it in GitHub Desktop.
Save tranngoclam/58920ecc59a0427b7b6625a234abd26b to your computer and use it in GitHub Desktop.
Example for fetching data from remote or local using RxJava
package net.lamtran.rxjava2;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
import rx.observers.TestSubscriber;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
/**
* Created by lam on 9/23/16.
*/
public class MultiSourcesTest {
StoreProvider mStoreProvider;
@After
public void reset() throws Exception {
mStoreProvider.reset();
mStoreProvider = null;
}
@Before
public void setup() throws Exception {
mStoreProvider = new StoreProvider();
}
@Test
public void testGetStoresFirstFromApi() throws Exception {
System.out.println("Test getting store with data coming from api");
TestSubscriber<List<Store>> testSubscriber = new TestSubscriber<>();
mStoreProvider.fetchStores().subscribe(testSubscriber);
testSubscriber.assertNoErrors();
testSubscriber.assertCompleted();
assertThat(testSubscriber.getOnNextEvents().size(), equalTo(1));
List<Store> stores = testSubscriber.getOnNextEvents().get(0);
assertThat(stores.size(), equalTo(10));
assertThat(StoreProvider.sStores.size(), equalTo(10));
}
@Test
public void testGetStoresFirstFromLocal() throws Exception {
System.out.println("Test getting store with data coming from local");
StoreProvider.sStores = mStoreProvider.getStores();
TestSubscriber<List<Store>> testSubscriber = new TestSubscriber<>();
mStoreProvider.fetchStores().subscribe(testSubscriber);
testSubscriber.assertNoErrors();
testSubscriber.assertCompleted();
assertThat(testSubscriber.getOnNextEvents().size(), equalTo(1));
List<Store> stores = testSubscriber.getOnNextEvents().get(0);
assertThat(stores.size(), equalTo(10));
assertThat(StoreProvider.sStores.size(), equalTo(10));
}
}
package net.lamtran.rxjava2;
/**
* Created by lam on 9/23/16.
*/
public class Store {
private final String address;
private final int id;
public Store(int id, String address) {
this.id = id;
this.address = address;
}
public String getAddress() {
return address;
}
public int getId() {
return id;
}
}
package net.lamtran.rxjava2;
import java.util.ArrayList;
import java.util.List;
import rx.Observable;
/**
* Created by lam on 9/23/16.
*/
public class StoreProvider {
public static List<Store> sStores;
public Observable<List<Store>> fetchStores() {
return Observable.concat(fetchFromLocal(), fetchFromApi())
.first(stores -> stores != null && !stores.isEmpty());
}
public List<Store> getStores() {
List<Store> stores = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
stores.add(new Store(i, "address " + i));
}
return stores;
}
public void reset() {
sStores = null;
}
private Observable<List<Store>> fetchFromApi() {
return Observable.defer(() -> {
System.out.println("- fetching from api");
List<Store> stores = getStores();
return Observable.just(stores);
}).doOnNext(stores -> {
System.out.println("- saving to local");
sStores = stores;
});
}
private Observable<List<Store>> fetchFromLocal() {
return Observable.defer(() -> {
System.out.println("- fetching from local");
return Observable.just(sStores);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment