Skip to content

Instantly share code, notes, and snippets.

@caseykulm
Last active August 30, 2015 03:10
Show Gist options
  • Save caseykulm/960b311569e4e169385b to your computer and use it in GitHub Desktop.
Save caseykulm/960b311569e4e169385b to your computer and use it in GitHub Desktop.
Spinner for Asynchronous and no spinner for Synchronous Observables
public class DataStore {
public static Observable<Something> getSomethingObservable() {
Something cachedSomething = MyCache.getCachedSomething();
if (cachedSomething != null) {
return Observable.just(cachedSomething); // still on same thread so returned to subscriber synchronously
}
return myRestService.getSomething(); // on separate Retrofit worker thread so returned to subscriber asynchronously
}
}
public class SomethingActivity extends Activity {
private Something mSomething;
private void getAndDisplayData() {
DataStore.getSomethingObservable()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(something -> {
mSomething = something;
showSomething();
});
if (mSomething == null) {
showSpinner(true);
}
}
private void showSomething() {
showSpinner(false);
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment