Skip to content

Instantly share code, notes, and snippets.

@TomohikoSato
Created January 10, 2017 05:59
Show Gist options
  • Save TomohikoSato/839c3802d931689957f45471aba5e060 to your computer and use it in GitHub Desktop.
Save TomohikoSato/839c3802d931689957f45471aba5e060 to your computer and use it in GitHub Desktop.
RxJavaハンズオン
# RxJava ハンズオン
---
# RxJavaとは
Reactive Extension for Java
https://github.com/ReactiveX/RxJava (Star 20000 以上)
イベントを作り、こねこねして、購読する
```
Observable.just("hogehoge") //作る
.map(s -> s.length()) //こねこねの例
.subscribe(i -> Log.d(TAG, Integer.toString(i))); // 購読する , 8
```
---
# ReactiveExtension
http://reactivex.io/
多様なプラットフォームで使われている
RxSwift, RxJS, UniRx, etc...
* マリオランはUniRxを採用
* AbemaTV iOS は SwiftRxを採用
---
## RxJava 採用アプリ例
* AWA
* Abema TV, Abema FRESH
* SoundCloud
* LINE, LINE BLOG, LINE MUSIC
* Apple Music
* Spotify
* Twitter, Periscope
* CookPad
* SNOW
* NicoBox, ニコニコ漫画, niconico event+
* はてなブックマーク
* Slack
* DroidKaigi 2016
* 大相撲
---
# 本題
イベントを作り、こねこねして、購読する
```
Observable.just("hogehoge") //作る
.map(s -> s.length()) //こねこねの例 文字列を文字列の長さに変換している
.subscribe(i -> Log.d(TAG, Integer.toString(i))); // 購読する , 8
```
* 作る : 非同期処理の結果(WEBAPIやDBのレスポンス)、イベント(Viewのクリックイベント)、リストの要素
* こねこね : 関数を適用、他のイベントと統合など
* 購読 : ActivityやFragmentでビューに反映など
---
### 非同期処理の例
```
new GithubRxWebClient().requestUser("square")
.subscribeOn(Schedulers.io()) // ワーカースレッドで実行
.observeOn(AndroidSchedulers.mainThread()) // メインスレッドで購読
.subscribe(user -> {/** ビュー反映*/}, throwable -> { /** エラー処理 Snackbar出すとかDialog出すとか */});
```
---
### イベント(Viewのクリックイベント)の例
未調査
RxBindingというライブラリを使うと便利らしい
https://github.com/JakeWharton/RxBinding
---
### リストの例
```
Observable.fromArray(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
.reduce((i, j) -> i + j)
.subscribe(k -> Log.d(TAG, String.valueOf(k)));
```
---
### 実際に動かす
https://github.com/TomohikoSato/RxJavaHandsOn
---
# 資料
* 公式
* http://reactivex.io/
* https://github.com/ReactiveX/RxJava
* Operatorが見やすい
* http://rxmarbles.com/
* 知見 (RxJava1系の資料もあるので適宜脳内変換する)
* https://speakerdeck.com/sys1yagi/rxjavaxue-xi-falsehesutohurakuteisutuhoimofalse
* http://www.slideshare.net/KazukiYoshida/droidkaigi-rxjava
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment