Last active
August 30, 2015 03:10
-
-
Save caseykulm/960b311569e4e169385b to your computer and use it in GitHub Desktop.
Spinner for Asynchronous and no spinner for Synchronous Observables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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