RxJava学習のベストプラクティスっぽいもの

800912a73ce4e879003b6c89cf80cfeb?s=47 yagi
March 17, 2015

 RxJava学習のベストプラクティスっぽいもの

800912a73ce4e879003b6c89cf80cfeb?s=128

yagi

March 17, 2015
Tweet

Transcript

  1. 3Y+BWBֶशͷϕετϓϥΫςΟεͬΆ͍΋ͷ 2015/03/17(Ր) ീ໦

  2. 3Y+BWBJTUPPIVHF

  3. 4UFQCZTUFQMFBSOJOH

  4. 4UFQCZTUFQBQQMJDBUJPO

  5. w ֓೦ w ֓೦ͷ+BWBԽ w ίϨΫγϣϯૢ࡞ w 1SPNJTF w -JGU

    w 4VCKFDU 3Y+BWB͸͜ͷॱͰֶ΂ʂʁ
  6. 3Y+BWBͱ͸ https://rx.codeplex.com/ https://github.com/ReactiveX/RxJava

  7. w 3FBDUJWF&YUFOTJPOT 3Y ͸ɺ؍ଌՄೳ PCTFSWBCMF ͳγʔέϯεͱ-*/2ελΠϧͷΫ ΤϦԋࢉࢠΛ࢖ͬͯɺඇಉظͳΠϕϯτϕʔεͷϓ ϩάϥϜΛ߹੒͢ΔϥΠϒϥϦͰ͢ɻ3YΛ࢖͏ͱɺ ։ൃऀ͸ඇಉظσʔλετϦʔϜΛ0CTFSWBCMF Ͱද͠ɺඇಉظσʔλετϦʔϜʹର͢ΔΫΤϦ

    ʹ-*/2ԋࢉࢠΛ࢖͍ɺඇಉظετϦʔϜͷฒߦ ੑΛ4DIFEVMFSͰύϥϝλԽ͠·͢ɻ؆୯ʹݴ͑ ͹ɺ3Y0CTFSWBCMF -*/2 4DIFEVMFSͰ͢ɻ 3Y+BWBͱ͸ http://okapies.hateblo.jp/entry/2015/03/04/031148
  8. w 0CTFSWBCMFͱ͸࣌ؒUʹྲྀΕΔ4USFBNͰ͋Δ ֓೦

  9. w 0CTFSWBCMFͱ͸࣌ؒUʹྲྀΕΔ4USFBNͰ͋Δ ֓೦ Event Array Async ͲΕ΋࣌ؒUʹྲྀΕΔ4USFBNͱͯ͠ߟ͑Δ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move Up Move Move
  19. ֓೦ͷ+BWBԽ ؔ਺Λద༻͢Δ 0CTFSWBCMF4USJOHUPVDI&WFOU-PH UPVDIFTNBQ OFX'VOD5PVDI&WFOU 4USJOH \ !0WFSSJEF QVCMJD4USJOHDBMM 5PVDI&WFOUUPVDI&WFOU

    \ SFUVSOUPVDI&WFOUUP4USJOH  ^ ^  Down Move Move Up “Move” “Move” “Down” “Up”
  20. 0CTFSWBCMFͷϊϦ͸Θ͔ͬͨɻ ͡Ό͋۩ମతʹԿʹ࢖͏ͷʁ

  21. w 0CTFSWBCMF͸Ϧονͳ*UFSBUPSͰ͋Δɻ ίϨΫγϣϯૢ࡞

  22. w 0CTFSWBCMF3FDJQFSFDJQFT0CTFSWBCMFGSPN SFDJQF-JTU  w 0CTFSWBCMF*OUFHFSDPVOUFS0CTFSWBCMFSBOHF    w

     ίϨΫγϣϯΛ0CTFSWBCMFԽ QVCMJDDMBTT999"EBQUFSFYUFOET"SSBZ"EBQUFS5\ QVCMJD0CTFSWBCMF5JUFNT \ SFUVSO0CTFSWBCMF SBOHF  HFU$PVOU  NBQ JHFU*UFN J  XJOEPX HFU$PVOU  UP#MPDLJOH  TJOHMF  ^ ^
  23. ͋ͱ͸ԿͰ΋΍Γ์୊ 0CTFSWBCMF3FDJQFIJTUPSZ0CTFSWBCMFGSPN IJTUPSZ-JTU  0CTFSWBCMF4USJOHJETIJTUPSZ NBQ OFX'VOD3FDJQF -POH \ !0WFSSJEF

    QVCMJD-POHDBMM 3FDJQFSFDJQF \ SFUVSOSFDJQFHFU*E  ^ ^  MJNJU   SFEVDF  OFX'VOD4USJOH -POH 4USJOH \ !0WFSSJEF QVCMJD4USJOHDBMM 4USJOHT -POHJE \ SFUVSO TOVMM    JEUP4USJOH  ^ ^  4USJOHJET4USJETUP#MPDLJOH TJOHMF 
  24. ͋ͱ͸ԿͰ΋΍Γ์୊ 0CTFSWBCMF3FDJQFIJTUPSZ0CTFSWBCMFGSPN IJTUPSZ-JTU  0CTFSWBCMF4USJOHJETIJTUPSZ NBQ OFX'VOD3FDJQF -POH \ !0WFSSJEF

    QVCMJD-POHDBMM 3FDJQFSFDJQF \ SFUVSOSFDJQFHFU*E  ^ ^  MJNJU   SFEVDF  OFX'VOD4USJOH -POH 4USJOH \ !0WFSSJEF QVCMJD4USJOHDBMM 4USJOHT -POHJE \ SFUVSO TOVMM    JEUP4USJOH  ^ ^  4USJOHJET4USJETUP#MPDLJOH TJOHMF  3FDJQF-POH ࠷େ݅ l zͰ࿈݁ ஋ΛऔΓग़͢
  25. ίϨΫγϣϯૢ࡞Ͱ௿ϦεΫֶश w ίϨΫγϣϯૢ࡞ʹద༻ͯ͠௿ϦεΫͰಋೖɻҰ୴ 3Y+BWBਥԽͯ͠΋࣮֐͸গͳ͍ɻ w ౷Ұతͳه๏ΛֶͿɺద༻ՕॴΛݕ౼͢Δ w Մಡੑͷߴ͍3Y+BWBίʔυΛ໛ࡧ͢Δ ྫ͑͹લทͷSFEVDF͸DPNNPOTMBOHͷ 4USJOH6UJMTKPJOΛ࢖ͬͨํ͕Α͘ͳ͍ʁͱ͔ͳΜͱ͔

     w ςετίʔυ௚ॻ͖Ͱࢼ͢ͱ௙Δ
  26. ׳Ε͖ͯͨͧɺ΋ͬͱ࣮༻ੑΛʂ

  27. w 0CTFSWBCMF͸1SPNJTFͰ͋Δ 1SPNJTF

  28. 1SPNJTF͸ͭͷ஋Λ࣋ͭ 0CTFSWBCMF http://ninjinkun.hatenablog.com/entry/introrxja

  29. 4DIFEVMFSͰεϨουΛม͑Δ result Observer 5ISFBE .BJO5ISFBE

  30. &SSPSIBOEMJOH Observer 5ISFBE .BJO5ISFBE

  31. ͭͷ஋Λ࣋ͭ0CTFSWBCMFΛ࡞Δ QVCMJD0CTFSWBCMF3FDJQFSFDJQF pOBMMPOHSFDJQF*E \ SFUVSO0CTFSWBCMFDSFBUF OFX0CTFSWBCMF0O4VCTDSJCF3FDJQF \ !0WFSSJEF QVCMJDWPJEDBMM 4VCTDSJC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