Last active
August 29, 2016 04:46
-
-
Save ersin-ertan/a3c6ac7951297ae3c20055d20ff7899b to your computer and use it in GitHub Desktop.
Type specific chaining with anonymous functions
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
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) { | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See: Stack overflow question
and: RxJava related issue
and: RxJava issued I raised about this
Todo:Find out why the twoflatMapCompletable
is required vianew Func1<Single<String>, Completable> ... new Func1<String, Completable>
instead of directly tonew Func1<String, Completable>
Result: Jake Wharton's answer of
Single<Single<String>>
andflatMap