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

RxSwiftのObservableとは何か | iOSDC Japan 2017

gomi_ningen
September 17, 2017

RxSwiftのObservableとは何か | iOSDC Japan 2017

2017/09/17 - iOSDC Japan 2017@早稲田大学 理工学部 西早稲田キャンパス
口頭発表原稿: http://qiita.com/gomi_ningen/items/c796c08fe672610beecf

gomi_ningen

September 17, 2017
Tweet

More Decks by gomi_ningen

Other Decks in Programming

Transcript

  1. 8 3FBDUJWF&YUFOTJPOT ͷओͳߏ੒ཁૉ 0CTFSWBCMF 0CTFSWBCMF 0CTFSWFS %JTBQPTBCMF  0QFSBUPS NBQ

    qBU.BQ pMUFS  4DIFEVMFS .BJO4DIFEVMFS ຊηογϣϯͰ͸3Yͷ࠷΋ಛ௃తͳߏ੒ཁૉͰ͋Δ 0CTFSWBCMF ͕ͲͷΑ͏ʹߏ੒͞Ε͍ͯΔ͔၆ᛌ͍ͯ͘͠   
  2. 12 ঢ়ଶભҠͱ௨஌ͷ໰୊ʹ͍ͭͯߟ͑Δ 2 ʮ"ʹมԽ͕ੜͨ͡Β#ʹ఻͍͑ͨʯ ͱ͖ɺ Ͳ͏͢Ε͹ Α͍Ͱ͠ΐ͏͔ʁ ʲྫ ʳ 

    ࣗ෼ͷ༧ఆ͕มΘͬͨͷͰ ʢঢ়ଶมԽʣ ɺ       ͓ళʹ༧໿Ωϟϯηϧͷ࿈བྷΛ͢Δ ʢ௨஌ʣ ʲྫ ʳ  ैۀһ͸ମௐ͕ѱ͘ͳͬͨͷͰ ʢঢ়ଶมԽʣ ɺ       ্࢘ʹձࣾΛٳΉ࿈བྷΛ͢Δ ʢ௨஌ʣ
  3. 16 ෳࡶͳ௨஌ͷ໰୊ʹ͍ͭͯߟ͑Δ 2 ʮ௨஌ݩ ʢ"ʣ ͷঢ়ଶมԽΛɺෳ਺ͷ௨஌ઌ ʢ# $ %ʣ ʹ఻͍͑ͨʯ

    ͱ͖ɺ Ͳ͏͢Ε͹Α͍Ͱ͠ΐ͏͔ʁ ໰୊͕΍΍ෳࡶʹͳΓɺҟͳͬͨෳ਺ͷ௨஌ઌʹঢ়ଶมԽ Λ௨஌͢Δඞཁ͕Ͱ͖ͯͨ
  4. 17 ෳࡶͳ௨஌ͷ໰୊ʹ͍ͭͯߟ͑Δ 2 ʮ௨஌ݩ ʢ"ʣ ͷঢ়ଶมԽΛɺෳ਺ͷ௨஌ઌ ʢ# $ %ʣ ʹ఻͍͑ͨʯ

    ͱ͖ɺ Ͳ͏͢Ε͹Α͍Ͱ͠ΐ͏͔ʁ ໰୊͕΍΍ෳࡶʹͳΓɺҟͳͬͨෳ਺ͷ௨஌ઌʹঢ়ଶมԽ Λ௨஌͢Δඞཁ͕Ͱ͖ͯͨ 変更という意識が加わった
  5. 18 ෳࡶͳ௨஌ͷ໰୊ʹ͍ͭͯߟ͑Δ 2 ʮ௨஌ݩ ʢ"ʣ ͷঢ়ଶมԽΛɺෳ਺ͷ௨஌ઌ ʢ# $ %ʣ ʹ఻͍͑ͨʯ

    ͱ͖ɺ Ͳ͏͢Ε͹Α͍Ͱ͠ΐ͏͔ʁ ʲྫʳ ༑ਓ਺ਓͱ༡Ϳ໿ଋΛ͍ͯͯ͠ɺ ஗ࠁͦ͠͏ͳͱ͖ # ͞Μ͸UXJUUFSɺ$͞Μ͸ి࿩ɺ%͞Μ͸ϝʔϧͰ࿈བྷ͠ ͳ͚Ε͹͍͚ͳ͍ঢ়گ
  6. 31 QVTIܕ0CTFSWFSύλʔϯ جຊతͳߏ଄͸ QVMM ܕ 0CTFSWFS ύλʔϯͱಉ͡ OPUJGZ ࣌ʹ஋΋౉ͯ͠͠·͏͜ͱʹΑΓɺ Ͳͷ஋ʹߋ৽

    ͞Εͨͷ͔ΛؚΜͩ৘ใ΋௨஌Ͱ͖ΔΑ͏ʹͨ͠΋ͷ OPUJGZ VQEBUFE7BMVF5 ͱ͍͏ܗʹͯ͋͛͠Δ ɾ ɾ ‎
  7. 33 4XJGU ͱδΣωϦοΫϓϩάϥϛϯά 4XJGU ͷ ϓ ϩ τ ί ϧ

    ͸ɺܕ ύ ϥ ϝ ʔ λ HFOFSJDUZQF QBSBNUFS Λ࣋ͯͳ͍ͱ͍͏࢓༷͕͋Δ ؔ࿈ܕ BTTPDJBUFEUZQF BCTUSBDUUZQFNFNCFS Λ࣋ ͭ͜ͱ͸Ͱ͖Δ ͜ͷύλʔϯͷ࣮૷͸ଞݴޠʹൺ΂΍΍࣮૷ͷݟ௨͠͸ ѱ͘ͳΔ ʢຊ࣭తͳҙຯ͸ಉ͡ʣ ɾ ɾ ‎
  8. 41 ʢ༨ஊʣ 4XJGU ͱந৅Ϋϥε  ϝιου ந৅Ϋϥε  ϝιου͸αϙʔτ͞Ε͍ͯͳ͍ 3Y4XJGU

    ͸ /FWFS ܕΛฦ͢ϝιουΛར༻ͯ͠ந৅ϝ ιουΛදݱ͍ͯ͠Δ ɾ ɾ
  9. 44 3FBDUJWF&YUFOTJPOT ͷ0CTFSWBCMF جຊతͳߏ଄͸ QVTI ܕ 0CTFSWFS ύλʔϯͱಉ͡ ҟͳΔ఺͸ओʹҎԼͷ 

    ఺    ɾ ɾ ௨஌͢Δ஋͸OFYU FSSPS DPNQMFUFEͱ͍͏จ຺͕ͭ ͍ͨ &WFOUʹͳΔ OFYU Ҏ֎͕௨஌͞ΕΔͱҎޙҰ੾௨஌͕͞Εͳ͘ͳΔ VOTVCTDSJCF ͷ੹຿Λ %JTQPTBCMF ͳΔΦϒδΣΫτ ʹ෼཭͍ͯ͠Δ
  10. 48 3Y ͷ0CTFSWBCMF ͕௨஌͢Δ஋ &WFOU ͷ࣮૷ 3Y4XJGUͷ0CTFSWBCMF͸ߋ৽͞Εͨ஋ OFYU ʹՃ͑ ͯɺΤϥʔ

    FSSPS ͱ׬ྃ DPNQMFUFE ͱ͍͏৘ใΛ 0CTFSWFSʹ௨஌͢Δ͜ͱ͕Ͱ͖·͢ ɾ
  11. 50 %JTQPTBCMF ϓϩτίϧͷఆٛ QVTI ܕ 0CTFSWFS ύλʔϯͷ VOTVCTDSJCF ΛൃՐͤ͞ Δ໾ׂΛ

    %JTQPTBCMF ͳΔϓϩτίϧʹ෼཭͢Δ ΞΫγϣϯΛൃՐͤ͞Δ͚ͩͳͷͰۃΊͯγϯϓϧͳΠ ϯλʔϑΣʔεʹͳΔ ɾ ɾ
  12. 56 ༨ஊ $PDPB ΞϓϦͰͷΞτϛοΫૢ࡞ $PDPB ͰͷΞτϛοΫૢ࡞͸ "EE *ODSFNFOU %FDSFNFOU 03

    "/% 903  $PNQBSFBOE4XBQ 5FTUBOE4FU 5FTUBOE$MFBS ࢀরͱ ʢඞཁͰ͋Ε͹ʣ ߋ৽ΛΞτϛοΫʹ΍Γ͍ͨ ࠓճ͸ *OU ͱ $PNQBSFBOE4XBQ Λ࢖ͬͯͦΕΛ࣮ ݱ͍ͯ͠Δ ɾ ɾ ɾ
  13. 59 0CTFSWFS ύλʔϯͱ 3Y ͷ 0CTFSWBCMF ͷൺֱ 0CTFSWFS ύλʔϯͱ͸ҟͳΓɺΑΓදݱྗ๛͔ͳ &WFOU

    Λ௨஌͢Δ ʢϝϦοτͰ͋ΓσϝϦοτͰ΋͋Δʣ QVTI ܕ 0CTFSWFS ύλʔϯͰ͸ߪಡղআΛߟ͑Δͱ݁ہ 0CTFSWFS ͕ 0CTFSWBCMF ʹԿΒ͔ͷํ๏ͰΞΫηεͰ ͖ͳ͚Ε͹ͳΒͳ͍͕ɺߪಡղআͷ࢓૊Έ͕ %JTQPTBCMF ʹ෼཭͞Εͨ͜ͱʹΑΓ໰୊͕ղফ͞Εͨ ɾ ɾ
  14. 61 0CTFSWFS Ͱ͋Γ 0CTFSWBCMF Ͱ͋Δ 4VCKFDU 4VCKFDU ͸ 0CTFSWFS 0CTFSWBCMF

    ྆ํͷੑ࣭Λ࣋ͭ ୅දతͳ΋ͷʹ 1VCMJTI4VCKFDU #FIBWJPS4VCKFDU ͳͲ ͕͋Δ ɾ ɾ
  15. 65 4VCTDSJQUJPO%JTQPTBCMF ʹٻΊΒΕΔ;Δ·͍ %JTQPTBCMFͷ੹຿͸୺తʹݴ͑͹VOTVCTDSJCFΞΫγϣ ϯΛݺͼग़͢ͱ͍͏΋ͷ 4VCTDSJQUJPO%JTQPTBCMF ͸ PCTFWFS ʹରԠ͢ΔΩʔͱ VOTVCTDSJCF

    Ͱ͖ΔΦϒδΣΫτࣗ਎ΛऑࢀরͰอ࣋͠ ͯ EJTQPTF ࣌ʹ PCTFSWBCMF VOTVCTDSJCF LFZ Λݺͼ ग़͢;Δ·͍Λ͍࣋ͬͯͯཉ͍͠ ɾ ɾ
  16. 71 ༨ஊ  ετϦʔϜఀࢭ࣌ͷߪಡͱ /PQ%JTQPTBCMF FSSPS DPNQMFUFE͕ඈΜͩ͋ͱ 0CTFSWBCMF Λߪಡ͠ ͯ΋ҙຯ͕ͳ͍

    ͜ͷͱ͖ʹ TVCTDSJCF ΛݺͿͱ PCTFSWFST ΁ͷ௥Ճ͕ߦ ΘΕͳ͍ͨΊߪಡղআͷඞཁ͕ͳ͘ TVCTDSJCF ͸Կ΋͠ ͳ͍ /PQ%JTQPTBCMF Λฦͤ͹Α͍ ɾ ɾ 71
  17. 72 ༨ஊ #BH ͷߏ଄ جຊతʹ 6*OU ஋Λ  ΦϦδϯ࿈൪ͰΩʔΛൃߦ *OTFSU

    ࣌ʹൃߦ͞Εͨ 6*OU ΩʔΛ༻͍ͯ <6*OU5> ͱ͍͏ܗͰΦϒδΣΫτΛอ࣋Ͱ͖ΔίϨΫγϣϯ Ծʹཧ૝Խ͞Εͨ()[$16 ͰΫϩοΫຖʹΩʔൃߦ ʢ͢ͳΘͪՃࢉॲཧʣ Λߦͬͨ৔߹Ͱ΋ɺ Φʔόʔϑϩʔ· Ͱ  ೥͔͔Δ ɾ ɾ ɾ
  18. 73 0CTFSWBCMF Λ؍࡯ͯ͠ࢥͬͨ͜ͱ Ұݟෳࡶͦ͏ʹΈ͑Δ 0CTFSBWCMF ͸ɺ ཁٻ͞Ε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