Created
December 24, 2018 12:02
-
-
Save YSRKEN/2c9358425542214cd310c66747fe3b82 to your computer and use it in GitHub Desktop.
フィーリングで覚えるRxJava入門 ref: https://qiita.com/YSRKEN/items/b1119ff352a957bd0e55
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 interface ICallback { | |
void onResponse(String data); | |
} | |
// コールバックを利用するメソッド | |
public class SampleApi { | |
void sendRequest(ICallback callback) { | |
callback.onResponse("test"); // 入れる値の例 | |
} | |
} |
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 class SampleClass { | |
// コールバックのインターフェースを継承したクラス | |
class TestCallback implements ICallback { | |
@Override | |
void onResponse(String data) { | |
mResult = data; // 利用方法の例 | |
} | |
} | |
private String mResult = ""; | |
private ICallback mCallback = new TestCallback(); | |
private SampleApi mSampleApi = new SampleApi(); | |
public sampleFunc() { | |
mSampleApi.sendRequest(mCallback); // ここで呼び出す | |
} | |
} |
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
// コールバックのインターフェースを継承したクラス | |
class TestCallback implements ICallback { | |
private ObservableEmitter<String> emitter; | |
// EmitterをDIするためのメソッド | |
public setEmitter(ObservableEmitter<String> emitter) { | |
this.emitter = emitter; // ここでDIしている | |
} | |
@Override | |
void onResponse(String data) { | |
// レスポンスの情報をonNextを通じてemitterに流し込む | |
emitter.onNext(data); | |
// 流し込みが完了したらonCompleteする | |
emitter.onComplete(); | |
} | |
} | |
// コールバックを利用するメソッドを利用するクラス | |
public class SampleClass { | |
private String mResult = ""; | |
private SampleApi mSampleApi = new SampleApi(); | |
public sampleFunc() { | |
// 一本のObservableを作成する | |
// (Javaはラムダ式については型推論するため、これはObservable<String>型になる) | |
Observable.create(emitter -> { | |
// コールバックを作成し、emitterを登録した後にAPIを実行する | |
// これにより、onResponseが発火し、onNextが実行された際に次のステップに進める | |
ICallback mCallback = new TestCallback(); | |
mCallback.setEmitter(emitter); | |
mSampleApi.sendRequest(mCallback); | |
// subcribe以降にObservableのメソッドチェーンで受け渡されたデータについての最終処理を記述する | |
}).subcribe(data -> { | |
// ここでdataはString型であることに注意(ObservableEmitter<String>からonNextで流されたデータ) | |
mResult = data; | |
}); | |
} | |
} |
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
Observable.create(emitter -> {処理1}) | |
.subscribeOn(Schedulers.computation()) | |
.observeOn(Schedulers.io()) | |
.map(data -> {処理2}) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(result -> 処理3}); |
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
Observable.create(emitter -> { | |
emitter.onNext(1); | |
emitter.onNext(2); | |
emitter.onNext(3); | |
emitter.onComplete(); | |
}).flatMap(data -> {func(data)}) | |
.subscribe(result -> 処理3}); |
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
Observable.create(emitter -> { | |
emitter.onNext(1); | |
emitter.onNext(2); | |
emitter.onNext(3); | |
emitter.onComplete(); | |
}) | |
.toList() | |
.flatMap(list -> { | |
Observable<Integer> task = null; | |
for (int x :list) { | |
if (task == null) { | |
task = func(x); | |
} else { | |
task = task.concatWith(func(x)); | |
} | |
} | |
return task; | |
}) | |
.subscribe(result -> 処理3}); |
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
Observable<Integer> obsA = Observable.create(emitter -> {略})); | |
Observable<Integer> obsB = Observable.create(emitter -> {略})); | |
Observable<Integer> obsC = Observable.zip(obsA, obsB, (d1, d2) -> d1 + d2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment