Skip to content

Instantly share code, notes, and snippets.

@ersin-ertan
Last active August 29, 2016 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ersin-ertan/a3c6ac7951297ae3c20055d20ff7899b to your computer and use it in GitHub Desktop.
Save ersin-ertan/a3c6ac7951297ae3c20055d20ff7899b to your computer and use it in GitHub Desktop.
Type specific chaining with anonymous functions
Api api = new Api() {
@Override Single<String> getKey() {
return Single.just("apiKey");
}
};
api.getKey().flatMap(new Func1<String, Single<String>>() {
@Override public Single<String> call(String key) {
return Single.create(new Single.OnSubscribe<String>() {
@Override public void call(final SingleSubscriber<? super String> singleSubscriber) {
Foo foo = new Foo();
foo.setAwesomeCallback(new AwesomeCallback() {
@Override public void onAwesomeReady(String awesome) {
try {
singleSubscriber.onSuccess(awesome);
} catch (Exception e) {
singleSubscriber.onError(e);
}
}
});
foo.makeAwesome();
}
});
}
}).flatMapCompletable(new Func1<String, Completable>() {
@Override public Completable call(final String string) {
return Completable.create(new Completable.CompletableOnSubscribe() {
@Override public void call(Completable.CompletableSubscriber completableSubscriber) {
try {
Test.this.sendAwesome(string);
completableSubscriber.onCompleted();
} catch (Exception e) {
completableSubscriber.onError(e);
}
}
});
}
})
.subscribe(new Action0() {
@Override public void call() {
Test.this.handleAwesomeSent();
}
}, new Action1<Throwable>() {
@Override public void call(Throwable throwable) {
}
});
@ersin-ertan
Copy link
Author

ersin-ertan commented Aug 26, 2016

See: Stack overflow question
and: RxJava related issue
and: RxJava issued I raised about this

Todo: Find out why the two flatMapCompletable is required via new Func1<Single<String>, Completable> ... new Func1<String, Completable> instead of directly to new Func1<String, Completable>

Result: Jake Wharton's answer of Single<Single<String>> and flatMap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment