create an observable from callback.
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 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