Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RxJava実践入門

 RxJava実践入門

Reactive Programmingの簡単な説明とAndroidアプリ開発において具体的にどのようにRxJavaを使うのかを説明します。

[参考文献]
- Reactive programming - Wikipedia, the free encyclopedia

https://en.wikipedia.org/wiki/Reactive_programming
- JJUG ナイトセミナーで Reactive Streams について発表しました

http://okapies.hateblo.jp/entry/2015/06/26/024505
- ReactiveX
http://reactivex.io/
- Exploring RxJava 2 for Android (GOTOcph October 2016)

https://speakerdeck.com/jakewharton/exploring-rxjava-2-for-android-gotocph-october-2016
- Reactive Programming with JDK 9 Flow API

https://community.oracle.com/docs/DOC-1006738

Hiroshi Kurokawa

January 17, 2018
Tweet

More Decks by Hiroshi Kurokawa

Other Decks in Technology

Transcript

  1. 4 3FBDUJWF1SPHSBNNJOHͱ͸ 5SBOTGPSNBUJPOBM A = 2 B = 3 C

    = (A + 1) x (B - 1) 3FBDUJWF   Y " # $   
  2. 5 3FBDUJWF1SPHSBNNJOHͱ͸ 5SBOTGPSNBUJPOBM 3FBDUJWF   Y " # $

       A = 2 B = 3 C = (A + 1) x (B - 1) A = 3
  3. 6 3FBDUJWF1SPHSBNNJOHͱ͸ 5SBOTGPSNBUJPOBM 3FBDUJWF   Y " # $

       A = 2 B = 3 C = (A + 1) x (B - 1) A = 3
  4. 7 3FBDUJWF1SPHSBNNJOHͱ͸ 5SBOTGPSNBUJPOBM 3FBDUJWF   Y " # $

       A = 2 B = 3 C = (A + 1) x (B - 1) A = 3
  5. 8 ͳͥ"OESPJEΞϓϦ։ൃͰ31͕஫໨͞ΕΔͷ͔ network.getDataA(new NetworkCall.Listener<String>() {
 @Override public void success(String data)

    { network.getDataB(data, new NetworkCall.Listener<String>() {
 @Override public void success(String data) { runOnUiThread(() -> { if (!isDestroyed()) { tv.setText(data);
 } });
 }‌
 
 @Override public void failure(Throwable t) {}
 });
 } @Override public void failure(Throwable t) {}
 });
  6. 18 3Yͱ͸ 1 2 3 4 5 6 < >

    GPSxJO JGx % 2 == 0UIFO QVTIx * x UP list FOE FOE SFUVSOlist
  7. 19 3Yͱ͸ 1 2 3 4 5 6 < >

    GJMUFS x % 2 == 0  NBQ x -> x * x
  8. 21 3Yͱ͸ 1 2 3 4 5 6 GJMUFS x

    % 2 == 0  NBQ x -> x * x
  9. 22 3Yͱ͸ 1 2 3 4 5 6 GJMUFS x

    % 2 == 0  NBQ x -> x * x 4 16 36
  10. 23 3Yͱ͸ 4 16 36 Observable.just(1, 2, 3, 4, 5,

    6)
 .filter(x -> x % 2 == 0)
 .map(x -> x * x)
 .subscribe(System.out ::println);
  11. 26 3Y+BWBͰͳʹ͕Ͱ͖Δͷ͔ @Override public void onCreate(Bundle savedInstanceState) { …
 subscription

    = network.getDataA()
 .subscribeOn(Schedulers.io())
 .flatMap(data -> network.getDataB(data))
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(
 data -> tv.setText(data),
 throwable -> Log.e(TAG, throwable.getMessage())
 );
 }
 @Override public void onDestroy() {
 super.onDestroy();
 subscription.unsubscribe();
 }
  12. 27 3Y+BWBͰͳʹ͕Ͱ͖Δͷ͔ @Override public void onCreate(Bundle savedInstanceState) { …
 subscription

    = network.getDataA()
 .subscribeOn(Schedulers.io())
 .flatMap(data -> network.getDataB(data))
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(
 data -> tv.setText(data),
 throwable -> Log.e(TAG, throwable.getMessage())
 );
 }
 @Override public void onDestroy() {
 super.onDestroy();
 subscription.unsubscribe();
 }
  13. 31 3Y+BWBͰͳʹ͕Ͱ͖Δͷ͔ private void setup() {
 input1.addTextChangedListener(new TextWatcher() {
 @Override

    public void onTextChanged(CharSequence s, int start, int before, int count) {
 input1Valid = s.length() > 0;
 validate();
 }
 });
 … }
 
 private void validate() {
 button.setEnabled(input1Valid && input2Valid && input3Valid);
 } 8JUIPVU3Y+BWB
  14. 32 3Y+BWBͰͳʹ͕Ͱ͖Δͷ͔ 8JUI3Y+BWB private void setup() {
 ArrayList<Observable<Boolean >> validators

    = new ArrayList <>();
 validators.add(RxTextView.textChanges(input1).map(s -> s.length() > 0));
 … 
 Observable.combineLatest(validators, args -> {
 for (Object a : args) {
 if (!(Boolean) a) return false;
 }
 return true;
 }).subscribe(button ::setEnabled);
 }
  15. 33 3Y+BWBͰͳʹ͕Ͱ͖Δͷ͔ 8JUI3Y+BWB private void setup() {
 ArrayList<Observable<Boolean >> validators

    = new ArrayList <>();
 validators.add(RxTextView.textChanges(input1).map(s -> s.length() > 0));
 … 
 Observable.combineLatest(validators, args -> {
 for (Object a : args) {
 if (!(Boolean) a) return false;
 }
 return true;
 }).subscribe(button ::setEnabled);
 }
  16. 34 3Y+BWBͰͳʹ͕Ͱ͖Δͷ͔ 8JUI3Y+BWB private void setup() {
 ArrayList<Observable<Boolean >> validators

    = new ArrayList <>();
 validators.add(RxTextView.textChanges(input1).map(s -> s.length() > 0));
 … 
 Observable.combineLatest(validators, args -> {
 for (Object a : args) {
 if (!(Boolean) a) return false;
 }
 return true;
 }).subscribe(button ::setEnabled);
 }
  17. 35 3Y+BWBͰͳʹ͕Ͱ͖Δͷ͔ 8JUI3Y+BWB private void setup() {
 ArrayList<Observable<Boolean >> validators

    = new ArrayList <>();
 validators.add(RxTextView.textChanges(input1).map(s -> s.length() > 0));
 … 
 Observable.combineLatest(validators, args -> {
 for (Object a : args) {
 if (!(Boolean) a) return false;
 }
 return true;
 }).subscribe(button ::setEnabled);
 }
  18. 40 3Y+BWBͰͳʹ͕Ͱ͖Δͷ͔ ConnectableObservable<Boolean> idling =
 RxAbsListView.scrollEvents(listView)
 .map(event -> event.scrollState()
 ==

    AbsListView.OnScrollListener.SCROLL_STATE_IDLE)
 .distinctUntilChanged()
 .publish(); أؙٗ٦ٕךꟚ㨣⨡姺׾嗚濼ׅ׷0CTFSWBCMF
  19. 44 3Y+BWBͰͳʹ͕Ͱ͖Δͷ͔ أؙٗ٦ָٕꟚ㨣׃׋׵ꬊず劍Ⳣ椚׾⨡姺ׅ׷ final Subscription s = idling.startWith(true)
 .filter(b ->

    b)
 .flatMap(aBool -> serivce.getData()) .takeUntil(idling.filter(b -> !b))
 .repeatWhen(completes -> completes.switchMap(
 c -> idling.filter(b -> b)))
 .subscribe(l -> showLog("Success!"));
  20. 45 3Y+BWBͰͳʹ͕Ͱ͖Δͷ͔ أؙٗ٦ָٕꟚ㨣׃׋׵ꬊず劍Ⳣ椚׾⨡姺ׅ׷ final Subscription s = idling.startWith(true)
 .filter(b ->

    b)
 .flatMap(aBool -> serivce.getData()) .takeUntil(idling.filter(b -> !b))
 .repeatWhen(completes -> completes.switchMap(
 c -> idling.filter(b -> b)))
 .subscribe(l -> showLog("Success!"));
  21. 47 3Y+BWBͰͳʹ͕Ͱ͖Δͷ͔ أؙٗ٦ָٕ⨡姺׃׋׵ⱄTVCTDSJCFׅ׷ final Subscription s = idling.startWith(true)
 .filter(b ->

    b)
 .flatMap(aBool -> serivce.getData()) .takeUntil(idling.filter(b -> !b))
 .repeatWhen(completes -> completes.switchMap(
 c -> idling.filter(b -> b)))
 .subscribe(l -> showLog("Success!"));
  22. 49 3Y+BWBͰͳʹ͕Ͱ͖Δͷ͔ 㹋遤ׅ׷أٖحس׾䭷㹀ׅ׷ final Subscription s = idling.startWith(true)
 .subscribeOn(Schedulers.io())
 .filter(b

    -> b)
 .flatMap(aBool -> serivce.getData())
 .takeUntil(idling.filter(b -> !b))
 .repeatWhen(completes -> completes.switchMap(
 c -> idling.filter(b -> b)))
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(l -> showLog("Success!"));
  23. 50 3Y+BWBͰͳʹ͕Ͱ͖Δͷ͔ ConnectableObservable<Boolean> idling =
 RxAbsListView.scrollEvents(listView)
 .map(event -> event.scrollState()
 ==

    AbsListView.OnScrollListener.SCROLL_STATE_IDLE)
 .distinctUntilChanged()
 .publish();
 subscriptions.add(idling.connect());
 
 subscriptions.add(idling.startWith(true)
 .subscribeOn(Schedulers.io())
 .filter(b -> b)
 .flatMap(aBool -> serivce.getData())
 .takeUntil(idling.filter(b -> !b))
 .repeatWhen(completes -> completes.switchMap( c -> idling.filter(b -> b)))
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(l -> showLog("Success!")));
  24. 52 ࢀߟ ˖ 3FBDUJWFQSPHSBNNJOH8JLJQFEJB UIFGSFFFODZDMPQFEJB
 IUUQTFOXJLJQFEJBPSHXJLJ3FBDUJWF@QSPHSBNNJOH ˖ ++6(ش؎زإىش٦ד3FBDUJWF4USFBNTחאְג涪邌׃ת׃׋
 IUUQPLBQJFTIBUFCMPKQFOUSZ ˖

    3FBDUJWF9
 IUUQSFBDUJWFYJP ˖ &YQMPSJOH3Y+BWBGPS"OESPJE (050DQI0DUPCFS 
 IUUQTTQFBLFSEFDLDPNKBLFXIBSUPOFYQMPSJOHSYKBWBGPS BOESPJEHPUPDQIPDUPCFS ˖ 3FBDUJWF1SPHSBNNJOHXJUI+%,'MPX"1*
 IUUQTDPNNVOJUZPSBDMFDPNEPDT%0$