è²·ç©æ å ±äºæ¥é¨ã®å «æ¨(@sys1yagi)ã§ãã
Androidçéã§RxJavaã話é¡ã«ãªã£ã¦ãã¾ãããã¯ãã¯ãããã¢ããª(以å¾ããæ¬ä½ãã¨è¡¨ç¾ãã¾ã)ã§ãå æ¥ã¤ãã«RxJavaã®å°å ¥ãæããã¾ãããæ¬ã¨ã³ããªã§ã¯RxJavaããã¼ã ã«å°å ¥ããçºã«è¡ã£ãããã¤ãã®åãçµã¿ãç´¹ä»ãã¾ãã
ç®æ¬¡
- RxJavaå°å ¥ã®å¤±æ
- ã©ã®ãããªèª²é¡ã解決ããã®ã
- å°å ¥ã®çºã«æ©è½ãå解ããå¦ç¿ã³ã¹ããèãã
- ããã°ãæ¸ã
- ä½ã³ã¹ããä½ãªã¹ã¯ã«å°å ¥ãã
- åå¼·ä¼ãéã
RxJavaå°å ¥ã®å¤±æ
2014å¹´11æã«RxJavaã®1.0.0ããªãªã¼ã¹ããã¾ãããéã«å®ç¨æ®µéãã¨ããäºã§å人çã«ãããã触ããæ¬ä½ã«å°å ¥ããæ©ä¼ã伺ã£ã¦ãã¾ãããããæ¥ãbug fixã®çºã«RxJavaã使ãã¨ç°¡æ½ã«ãªãã®ã§ã¯ãªããã¨æãæ°è»½ã«Pull Request(以å¾ãPRã¨ãã¾ã)ãéã£ãæãããã®ã¿ã¤ãã³ã°ã§æ¥ã«å°å ¥ããæå³ã¯ãªã«ãï¼ããRxJavaã§ãªããã°ãããªãçç±ã¯ä½ãï¼ãããã¡ãã¨å¦ç¿ãã¦ããã¡ã³ãã¼ãå°ãªãä¸ã§å°å ¥ãã¦ã¡ã³ãã¯å¤§ä¸å¤«ãï¼ãã¨ãã£ãçªã£è¾¼ã¿ãå ¥ããPRããããã2æéã»ã©ã§RxJavaã¨ãå¥ãã¨ãªãã¾ããã
ã¬ãã¥ã¼ã§ã®çªã£è¾¼ã¿ã¯ãã£ã¨ãã§ãæ確ãªçç±ããªãã¾ã¾ãã¼ã å ¨ä½ã¨ãã¦å¦ç¿ãä¸ååãªã©ã¤ãã©ãªãæã¡è¾¼ãã®ã¯ã¾ããã§ããããã¡ãªãããããªã¹ã¯ã®æ¹ãé«ããã§ãã
ã©ã®ãããªèª²é¡ã解決ããã®ã
å°å ¥ã«å¤±æããç§ã¯åçããã¾ãã¯RxJavaã«ãã£ã¦æ¬ä½éçºã®ã©ã®ãããªèª²é¡ã解決ã§ããããªã®ããæ´ãåºãã¾ããã
No | èª²é¡ | ã©ã®ããã«è§£æ±ºã§ããã |
---|---|---|
1 | éåæå¦çç³»ã®æ¸ãæ¹ã«çµ±ä¸æ§ããªããè¤æ°ã®éåæå¦çãåæããçºã®ãã¾ãããæ¹ãå®ã¾ã£ã¦ããªã | RxJavaãPromiseã¨ãã¦ä½¿ãäºã§åä¸ã®æ©è½ãæã¤éåæå¦ç群ãæ§æã§ãããããããã¡ã¤ã³ã«åããã¦åæããã¹ã¿ã¤ã«ã«ããã°ãAsyncTaskå ã§CountDownLatchã§å¾ ã¡åããããããªã¹ã¿ã¤ã«ãé§éã§ãã |
2 | Fragmentã§getActivity() == null ãªã©ã®ç¶æ
ãã§ãã¯æ¼ãã«ãã£ã¦ã¯ã©ãã·ã¥ããåé¡ |
Fragmentã®ã©ã¤ããµã¤ã¯ã«ã«åããã¦unsbscribeããæ©æ§ãä½ããRxAndroid(RxJavaã使ã£ã¦Androidç¨ã«æ¸ãããã³ã³ãã¼ãã³ã群ã§ã)çã使ãäºã§ç¶æ ãã§ãã¯ãä¸è¦ã«ãªã |
ã¾ããå°å ¥ã«ãã£ã¦æå¾ ã§ããå¹æã«ã¤ãã¦ãæ¤è¨ãã¾ããã
No | æå¾ ã§ããå¹æ | çç± |
---|---|---|
1 | è¤éãªãã¸ãã¯ãçµ±ä¸çãªè¨è¿°ã«ã§ãã | Observableåããå¾ã®è¨è¿°ã¯RxJavaã«æ²¿ãã®ã§çµ±ä¸çã«ãªããã¾ãä¸èªç¶ãªæ¸ãæ¹ãç¡çããæ¸ããå ´åãªã©ã®æ¤åºããããã |
2 | ã¡ã³ããããªãã£ã®åä¸ | åå¦çãOperatoråä½ã§è¨è¿°ããã®ã§å¦çã®æå³ãç解ãããããªã |
3 | ã¢ããªã±ã¼ã·ã§ã³ã¢ã¼ããã¯ãã£ã®æ¹å | ç¾å¨ã®ã¢ã¼ããã¯ãã£ãæ¹åãã¦ããéã«RxJavaã®ææ³ãå¿ç¨ã§ããã®ã§ã¯ãªãã |
ãæå¾ ã§ããå¹æãã®æ¹ã¯ã¨ãããã課é¡ã®è§£æ±ºã«æå¹ã¨ããäºã§ããã°å¦ç¿ã³ã¹ããæ¯æã価å¤ã¯ååããã§ãããããªããªãä¸è¨ã®2ã¤ã®èª²é¡ã¯æ¬ä½éçºã«ããã¦ãã£ã¨æå¹ãªæ段ãå¾ãããªãã¾ã¾ã§ããããã§ããããã«ã§ãå°å ¥ãããæã§ããæ¬å½ã«èª²é¡ã®è§£æ±ºã«æå¹ãã©ããã«ã¤ãã¦ã©ã®ããã«å¤æããã°ããã§ãããããããã¯RxJavaãå¦ç¿ãé©ç¨ããªããæ¤è¨¼ãããããªãã§ããããã®è¾ºããå¦ç¿ã³ã¹ãã®å¤§ããRxJavaã®ã¸ã¬ã³ãã¨ãªãã¾ããæ°è»½ã«å§ãããã¦ãããããã°å¼ãè¿ãããããªå ¥ãå£ãæ¢ãå¿ è¦ãããã¾ãã
å°å ¥ã®çºã«æ©è½ãå解ããå¦ç¿ã³ã¹ããèãã
ããã§ãæ©è½ãå解ãã¦å¦ç¿ã³ã¹ããæããããã¢ããã¼ãããªããæ¢ãäºã«ãã¾ããã
RxJavaã®æ©è½ã®æ´»ç¨æ¹æ³ã¯æ¦ã以ä¸ã®æ§ã«å解ã§ãã¾ãã
- Listå¦çã®æ½è±¡åã»ã¹ããªã¼ã å
- Optional
- Promise
- Data Binding
- Event Bus
ãããã®ãã¡ãå¦ç¿ã³ã¹ããæããããããªã®ã¯Listå¦çã§ã¯ãªãããªã¨æãã¾ããOptionalã¯ã¢ããªã±ã¼ã·ã§ã³ã®è¨è¨ã«å¤§ããé¢ãã£ã¦ããã®ã§Optionalãã®ãã®ã®è°è«ãå¿ è¦ã«ãªãã¾ãããPromiseã¯ã¹ã¬ããæä½å¨ããæèããå¿ è¦ããããããªãåãçµãã«ã¯ã³ã¹ããé«ããã§ããData BindingãEvent Busã¯Subjectãå©ç¨ããã®ã§ãObservableãç¥ãåã«è§¦ããã®ã¯ãªã¹ã¹ã¡ã§ãã¾ãããã³ã¼ããæ¸ããªããObservableã¨Operatorã®é¢ä¿ã«è§¦ãã¤ã¤ãä½ã³ã¹ãã»ä½ãªã¹ã¯ã«å¦ã¶ã«ã¯Listå¦çãä¸çªããããã§ãããããRxJavaã¯ãã¡ããããã¨ããå¤æã«ãªã£ãæããListæä½ã§ã®é©ç¨ã ããªãæ¯è¼çç°¡åã«æ»ããã§ãããã
ããã°ãæ¸ã
RxJavaå°å ¥ã«ãã£ã¦è§£æ±ºåºæ¥ãããªèª²é¡ã¨ãä½ã³ã¹ãã§å¦ç¿ã§ããããªæ¹æ³ãå®ã¾ã£ãã®ã§æ¬¡ã«ããããã¾ã¨ããã¨ã³ããªã社å ããã°ã«æ¸ãã¾ããã社å ã®ããã°ãªã®ã§ããã«ã¯è¼ãããã¾ããããæ¦ãåããããªå 容ãå人ããã°(RxJavaãã³ã¬ã¯ã·ã§ã³æä½ã©ã¤ãã©ãªã¨ãã¦æããã°å¦ç¿ã³ã¹ãã¨å°å ¥ãªã¹ã¯ãä½æ¸ã§ããã®ã§ã¯ãªãã)ã«æ¸ãã¦ãã¾ãã
社å ããã°ã®ã¨ã³ããªã§ã¯ãæ¬ä½éçºã«ããã¦RxJavaãå°å ¥ããäºã§æå¾ ã§ããããªç¹ã«ã¤ãã¦è¨åãã¤ã¤ãObservableã®æ¦è¦ã¨åºæ¬çãªä½¿ãæ¹ãListæä½ããã¼ã¹ã«è§£èª¬ãã¾ãããã¾ãã¨ã³ããªã®æ«å°¾ã§RxJavaãå°å ¥ããPRãéãã¨ãã宣è¨ããã¾ããã
ä½ã³ã¹ããä½ãªã¹ã¯ã«å°å ¥ãã
社å
ããã°ãå¨ç¥ãããã¨ãããã°ã§ã®å®£è¨éãListæä½å¨ãã®å®è£
ãç½®ãæããPRãéãã¾ãããå¤æ´å¯¾è±¡ã¯ListUtils
ã¨ãããªã¹ãæä½å¨ãã®ã¦ã¼ãã£ãªãã£ã¯ã©ã¹ã§ãT head(List<T>)
, List<T> tail(List<T>)
ãªã©ã®å®è£
ãRxJavaã使ã£ã¦ç½®ãæãã¾ããã
ãããã®å®è£ ã¯å ã ãã£ããã®ãªã®ã§æ»ããã¨æãã°ãã¤ã§ãæ»ãäºãã§ããä½ãªã¹ã¯ã§ããåæã«ç¹ã«ã¡ãªãããç¡ãã®ã§ããããRxJavaãé©ç¨ããã³ã¼ããã¬ãã¥ã¼ãããã¨ããæ©ä¼ãä½ãã®ã«ã¯æé©ã§ãã
ãã®PRããã¼ã¸ããå¾ããã¸ãã¹ãã¸ãã¯é¨åã®Listæä½å¨ããä¸å¿ã«RxJavaãé©ç¨ãé²ãã¦ããã¾ããããã®ä¸ã§åé·ãªé¨åããããã«ããé¨åãã¬ãã¥ã¼ã§ææãã¦ãããæ¹åãã¦ããã¾ããã
åå¼·ä¼ãéã
ããç¨åº¦çãRxJavaã³ã¼ãã®ã¬ãã¥ã¼ã«æ £ãã¦æ¥ã段éã§åå¼·ä¼ãéãäºã«ãã¾ãããçºè¡¨å 容ã¯åç´ããå¿ç¨ã¾ã§æ§ã ã§ãçºè¡¨è ã®å¤ããçºè¡¨ã®çºã«RxJavaã®èª¿æ»ã«åãå ¥ãã¦ãã¾ãããããããåå¼·ä¼é§åã£ã¦ãã¤ã§ãããå½åã¯é¢ä¿è 6,7人ã§éãã«ãããã¨èãã¦ãã¾ããããã©ãã©ãåå è ãè¨ãã¿æçµçã«ã¯20åãããã¾ã§ã«ãªãã¾ããã
ãã®åå¼·ä¼ã«ãã£ã¦ãçºè¡¨è ã¯ããRxJavaã®å¦ç¿ãããé²ã¿ãåå è ãåºæ¬çãªé¨åã®ç解ãå¦ç¿ã®ãã£ãããã¤ãããããã«æãã¾ãã
以ä¸ã¯åå¼·ä¼ã®éã«çºè¡¨ããè³æã§ããå ¬éããã¦ãããã®ã«ã¤ãã¦åæãã¾ãã
- ããããããªããã©æè¿ã¤ãã£ã¦ãã²ã¼ã 㧠RxJava ã¤ãã£ã¦ã¿ã¦ã
- RxJavaçãªãã®ãæå°éã«å®è£ ãã¦ã³ã³ãã¼ãã³ãã®é¢ä¿ãç解ãã
- 7ã¤ã®ãµã³ãã«ããã°ã©ã ã§å¦ã¶RxJavaã®æå â»åå¼·ä¼ã§ã®çºè¡¨ã¨ã¯è¥å¹²ç°ãªãã¾ããå 容ã¯æ¦ãåãã§ã
- RxJavaå¦ç¿ã®ããã¹ãããã©ã¯ãã£ã¹ã£ã»ãããã®
ãããã«
åå¼·ä¼å¾ãæ¬ä½éçºã«RxJavaãç¨ããããæ©ä¼ãå¢ãã¾ãããç¾å¨ã§ã¯API callå¨ãã®Observableã使ã£ãPromiseåãé²ãã§ãã¾ããAPI callã®ä¸¦ååãç´ååã®æ¸ãæ¹ããå®è¡ã¹ã¬ããã®è¨å®ãã©ãã§æ¸ããã¨ãã£ã話ããå¯èªæ§ãªã©ã«ã¤ãã¦æ¥ã ã¬ãã¥ã¼ã®ä¸ã§è°è«ãã¦ãã¾ããããããã¯Data BindingãEvent Busçã®é©ç¨ã«ã¤ãã¦ãæ¤è¨ããã¦ããã§ããããã¾ãã解決ã§ãã課é¡ã¨ãã¦æãã£ãéåæå¦çãã©ã¤ããµã¤ã¯ã«ã®åé¡ã«å¯¾ãã¦ãå¾ã ã«RxJavaãé©ç¨ãã¤ã¤ããã¾ããä»ã®ã¨ãã大ããªåé¡ã¯ã§ã¦ãããæå¾ éãã®å¹æãå¾ããã¦ãã¾ãã
以ä¸ãAndroidéçºã§RxJavaããã¼ã ã«å°å ¥ããçºã«è¡ã£ãåãçµã¿ã§ããæ¬ã¨ã³ããªãRxJavaå°å ¥ã®æ¤è¨ã®å½¹ã«ç«ã¦ã°å¹¸ãã§ãã
ãããªã¯ãã¯ãããã§ã¯ã¦ã¼ã¶ã¼ã®èª²é¡ãã©ããã風ã«ã¢ãã¤ã«ã§è§£æ±ºããããèããå®ç¾ããããã«æ ç±ã注ããæ¹ãåéãã¦ãã¾ãï¼èå³ãããæ¹ã¯ãã²ãå¿åãã ããã