Skip to content

Instantly share code, notes, and snippets.

@tcw165
Created March 29, 2016 17:35
Show Gist options
  • Save tcw165/0e4e3dc0e4e001c7aa2b0297d752ff4e to your computer and use it in GitHub Desktop.
Save tcw165/0e4e3dc0e4e001c7aa2b0297d752ff4e to your computer and use it in GitHub Desktop.
_log("Before Observable");
// Create a "cold" observable.
Observable<Boolean> ob = Observable
// The "just" convert a boolean into an Observable.
.just(true)
// Transform the items emitted by an Observable by applying a function to each item.
.map(new Func1<Boolean, Boolean>() {
@Override
public Boolean call(Boolean aBoolean) {
_log("Within Observable");
_doSomeLongOperation_thatBlocksCurrentThread();
return aBoolean;
}
})
// Run Observable asynchronously.
.subscribeOn(Schedulers.io())
// Emit the items to subscribers on main thread.
.observeOn(AndroidSchedulers.mainThread());
// Observe and get the subscription (like a voucher).
Subscription subscription = ob
.subscribe(new Action1<Boolean>() {
@Override
public void call(Boolean bool) {
_log(String.format("Call with return value \"%b\"", bool));
}
});
_log("Right after Observable");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment