Skip to content

Instantly share code, notes, and snippets.

@jiangecho
Created December 28, 2015 13:19
Show Gist options
  • Save jiangecho/1cd6d8df0cbcb694d380 to your computer and use it in GitHub Desktop.
Save jiangecho/1cd6d8df0cbcb694d380 to your computer and use it in GitHub Desktop.
create an observable from callback.
public Observable<String> doAmazingThingObservable(int param) {
return Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
doAmazingThing(param, new CallBack() {
@Override
public void onGotResult(String result) {
if (result == null) {
if (!subscriber.isUnsubscribed()) {
subscriber.onError(new SomeException());
}
} else {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(result);
subscriber.onCompleted();
}
}
}
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment