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

RxExampleから学ぶ!RxSwift

 RxExampleから学ぶ!RxSwift

8月19日 RxSwift勉強会の発表資料です!

B6ce8bebf30b97bac7624b0c6622352e?s=128

Kazuhiro Sakamoto

August 19, 2016
Tweet

Transcript

  1. RxExample͔ΒֶͿʂ RxSwift @kazu0620

  2. ࣗݾ঺հ ࡔຊ ࿨େ( @kazu0620 ) Sansanגࣜձࣾ Eightࣄۀ෦ॴଐ աڈʹݸਓͰ։ൃͨ͠ΞϓϦ - ൿີͷΞϧόϜ(40ສDL!)

    - ʹΌΜ͜(15ສDL!)
  3. RxExampleͱ͸ʁ

  4. RxExample • ެࣜϦϙδτϦʹؚ·ΕͯΔRxSwiftͷ࢖༻ྫ • ඇৗʹγϯϓϧͳྫ͔ΒɺMVVMͰهड़͞Ε ࣮ͨ੎తͳྫ·Ͱɺ਺ଟ͘ͷαϯϓϧ͕͋Δɻ • RxSwiftΛͲ͏࢖͑͹ྑ͍͔ɺͲ͏࢖ͬͯ΄͠ ͍ͱ૝૾ͯ͠࡞ΒΕ͍ͯΔͷ͔Θ͔Δ

  5. git clone git@github.com:ReactiveX/RxSwift.git Ҏ߱ͷ͓࿩͸ͥͻɺRxExampleͷίʔυ Λݟͳ͕Β͓ฉ͖͍ͩ͘͞ɻ

  6. ͷExamplesҎԼͷσΟϨΫτϦɻ

  7. Numbers

  8. 3ͭͷTextFieldʹͦΕͧΕ਺஋Λೖྗ͢Δͱɺ ϦΞϧλΠϜʹͦͷ߹ܭ͕ग़ྗ͞ΕΔɻ

  9. None
  10. rx_text: TextFieldͷtextʹมߋ͕͋ͬͨ৔߹ʹͦͷ஋Λemit͢Δɻྫ ͑͹શͯͷTextFieldʹ1,2,3ͱ͍͏਺஋Λॱʹೖྗͨ͠ͱ͖͸ɺԼهͷ ༷ͳγʔέϯεͱͳΔɻ

  11. combineLatest: ͦΕͧΕͷγʔέϯεͷ࠷ޙʹൃߦͨ͠஋Λ߹੒͠ ͯɺ৽͍͠γʔέϯεΛੜ੒͢ΔɻʮͲͷγʔέϯεΛ߹੒͢Δ͔ʁʯ ͱʮͲ͏΍ͬͯ߹੒͢Δ͔ʁʢΫϩʔδϟʹॻ͘)Λࢦఆ͢Δ͜ͱ͕Ͱ ͖Δɻ + +

  12. map: ετϦʔϜͷཁૉΛม׵͠ɺ৽ͨͳετϦʔϜΛੜ੒͢Δɻ഑ྻʹର ͯ͠mapΛ࣮ߦ͢Δͷͱಉ͡Α͏ʹߟ͑ΔͱΘ͔Γ΍͍͢ɻ্هͷmap͸ combineLatest͕ฦ͢ཁૉ͸਺஋ܕͳͷͰɺͦΕΛdescriptionΛ༻͍ͯString ʹม׵͍ͯ͠Δɻ

  13. bindTo: ετϦʔϜͷཁૉ͕ྲྀΕͯ͘ΔͷΛߪಡ͠ɺͦͷཁૉΛର ৅(UI෦඼ͳͲ)ʹඥ෇͚Δɻͱ͍͏ॲཧΛ؆୯ʹॻ͚Δɻ͘͢͝ஸೡ ʹॻ͘ͱɺԼهͱಉ͜͡ͱΛ͍ͯ͠Δɻ

  14. subscribe: ετϦʔϜʹྲྀΕͯ͘ΔཁૉΛߪಡ͢Δ͜ͱ͕ग़དྷΔɻ ਖ਼ৗʹ஋͕ྲྀΕ͖ͯͨ৔߹: .Next Τϥʔ͕ൃੜͨ͠৔߹: .Error ετϦʔϜ͕ऴྃͨ͠৔߹: .Completed subscribeNext, subscribeError

    ͳͲͰΠϕϯτΛࢦఆͯ͠ߪಡ ͢Δ͜ͱ΋Մೳɻ
  15. addDisposableTo: ߪಡͷഁغΛ؅׋͢ΔDisposeBagΛࢦఆ ͢Δɻ͜Ε͕ͳ͍ͱɺετϦʔϜΛӬԕʹߪಡͨ͠··ʹͳͬͯ͠· ͏৔߹͕͋ΔͷͰ஫ҙɻ DisposeBag: bagΛอ࣋͢ΔΠϯελϯε͕dealloc͞ΕͨλΠϛ ϯάͰɺอ࣋͢Δ͢΂ͯͷߪಡΛղ์͢Δɻͭ·ΓɺNumbersͷྫͰ ݴ͏ͱViewController͕dealloc͞ΕΔλΠϛϯάͰɺετϦʔϜͷߪ ಡ΋͢΂ͯऴྃ͢Δɻ

  16. SimpleValidation

  17. UsernameɺPasswordͷTextFieldʹೖྗ͞Εͨจ ࣈ਺ʹΑͬͯಈతʹද͕ࣔมԽ͢Δʢ੺͍ܯࠂจ ࣈ͕ফ͑Δ / Ϙλϯ͕disable͔ΒenableʹͳΔʣ

  18. map(෮श): ετϦʔϜʹྲྀΕͯ͘Δཁૉ͸ɺrx_textͳͷͰจࣈ ྻɻͰ΋ࠓճ͸ɺ࠷ऴతʹ͸͜ΕΛhidden(BOOL)ͱ͔ enabled(BOOL)ʹɺbind͍ͨ͠ͱ͍͏اըɻͳͷͰɺmapΛར༻͠ ͯཁૉΛString͔Β5จࣈҎ্͔Ͳ͏͔(BOOL)ʹม׵͍ͯ͠Δɻ

  19. ͜͜Ͱ͸ઌ΄Ͳ࡞ͬͨusernameValidͱpasswordValidͷετϦʔϜ Λ߹੒ͯ͠ɺ͍ͣΕ΋Trueͩͬͨͱ͖ͷΈTrueΛྲྀ͢ετϦʔϜΛ৽ ͨʹੜ੒͍ͯ͠Δɻ combineLatest: ͦΕͧΕͷγʔέϯεͷ࠷ޙʹൃߦͨ͠஋Λ߹੒͠ ͯɺ৽͍͠γʔέϯεΛੜ੒͢ΔɻʮͲͷγʔέϯεΛ߹੒͢Δ͔ʁʯ ͱʮͲ͏΍ͬͯ߹੒͢Δ͔ʁʢΫϩʔδϟʹॻ͘)Λࢦఆ͢Δ͜ͱ͕Ͱ ͖Δɻ

  20. bindTo(෮श): ετϦʔϜͷཁૉΛUIViewͷϓϩύςΟʹඥ෇͚͍ͯΔɻ addDisposableTo: (෮श): ετϦʔϜͷߪಡऴྃΛ؅ཧ͢ΔDisposeBag ʹͦΕͧΕͷߪಡΛొ࿥͍ͯ͠Δɻ

  21. subscribeNext(෮श): ετϦʔϜʹྲྀΕͯ͘ΔΠϕϯτ(ࠓճ͸ UIButtonͷλοϓ)Λߪಡ͠ɺϋϯυϦϯά͢Δɻ

  22. গ͠࿩Λ໭͢

  23. shareReplay: ετϦʔϜΛHotม׵͠ɺ෼ذ͍ͤͯ͞Δɻ

  24. Observable • γʔέϯεͷܭࢉํ๏/खଓΛఆٛͨ͠΋ͷɻ ఆ͚ٛͨͩ͠ͳͷͰɺߪಡ(subscribe)͢Δ· Ͱ͸࣮ߦ͞ΕΔ͜ͱ͸ͳ͍ɻٯʹݴ͏ͱɺ subscribe͞Εͨ࣌఺Ͱఆٛͨ͠ܭࢉ͕࣮ߦ͞ ΕΔɻ

  25. bindTo(෮श): ΍͍ͬͯΔ͜ͱ͸ɺsubscribeͯ͠.NextͰର৅ͷUIViewͷ ϓϩύςΟΛมߋ͍ͯ͠Δͷͱಉ͡ɻ

  26. shareReplay͍ͯ͠ͳ͍ͱ͖

  27. ্هͷίʔυ͸ಉҰͷObservableΛ2ճߪಡ(subscribe)͍ͯ͠Δɻઌ ΄Ͳઆ໌ͨ͠Α͏ʹɺObservable͸ subscribe͞Εͨ࣌఺Ͱఆٛͨ͠ܭࢉ͕࣮ߦ͞ΕΔ ͭ·Γ2ճsubscribe͢Δͱɺ2ຊͷετϦʔϜ͕ੜ· Εɺ2ճܭࢉ͕࣮ߦ͞ΕΔ

  28. shareReplay͕ͳ͍৔߹ɺ͜ͷmap͕2ճ(subscribeͨ͠਺͚ͩ)࣮ߦ͞ Εͯ͠·͏ɻ ͜Ε͘Β͍ͷॲཧͳΒͨ͘͞Μ૸ͬͯ΋໰୊ͳ͍͕ɺ৔߹ʹΑͬͯ͸ ҙਤͤͣແବͳܭࢉϦιʔεΛ࢖ͬͯ͠·͏͜ͱʹͳΔɻ

  29. HotͱCold

  30. ShareReplay͍ͯ͠ͳ͍Observable ShareReplayͨ͠Observable ্هͷਤ͸ʮRxͷHotͱColdʹ͍ͭͯʯΑΓҾ༻ (http://qiita.com/toRisouP/items/f6088963037bfda658d3)

  31. SimpleTableViewExample

  32. γϯϓϧͳTableViewΛRxSwiftͷbindingػߏΛ ࢖࣮ͬͯݱ͢Δɻ

  33. ഑ྻΛObservableʹแΉɻ͜͜Ͱ͸SampleͳͷͰ؆қʹ഑ྻΛཁૉ ͱͯ࣋ͬͨ͠ObservableΛੜ੒͢ΔͨΊ্هͷΑ͏ʹهड़͍ͯ͠Δͩ ͚ɻ ࣮ࡍʹ͸ɺAPI௨৴ʹΑͬͯऔಘͨ͠഑ྻΛཁૉͱͯ࣋ͭ͠ ObservableΛTableViewʹbind͢ΔɺͳͲͷέʔε͕ଟ͍͸ͣɻ

  34. ഑ྻΛཁૉͱͯ࣋ͬͨ͠ObservableΛtableViewʹbind͍ͯ͠Δɻ͜ Ε͚ͩͰTableViewͷද͕ࣔͰ͖Δɻ CellͷIdentifierͱܕΛҾ਺ʹ౉͠ɺΫϩʔδϟ಺ͰCellͷॳظԽॲཧ Λ࣮૷͢Δ͚ͩɻ row: IndexPath.row element: bind͞ΕͨObservableͷཁૉ(ࠓճ͸จࣈྻ) cell: ੜ੒͞ΕͨTableViewCell

  35. rx_modelSelected: Cell͕Select͞Εͨ࣌ͷΠϕϯτΛemit͢ΔɻҾ਺ (String)͸bindͨ͠ཁૉͷܕΛࢦఆ͢Δɻ rx_itemAccessoryButtonTapped: Cellʹ഑ஔ͞Ε͍ͯΔ AccessoryButton͕λοϓ͞Εͨ࣌ͷΠϕϯτΛemitɻ

  36. GitHubSignup(Driver൛)

  37. GitHubͷΞΧ΢ϯτΛ৽͘͠ొ࿥͢ΔॲཧΛRxSwiftͰ࣮૷ (ΞΧ΢ϯτͷੜ੒෦෼͸MockͳͷͰຊ౰ʹΞΧ΢ϯτ͕࡞ΒΕΔ͜ͱ͸ͳ͍) ɾDriverΛ༻͍ͨUIύʔπ΁ͷόΠϯσΟϯάॲཧ ɾViewModelΛ༻͍ͨϓϨθϯςʔγϣϯϩδοΫͷ෼཭ ɾAPIϦΫΤετΛ࣮ߦ͢Δ௨৴ॲཧ ΈͲ͜Ζ

  38. Driverͱ͸ʁ

  39. Driver ObservableΛUIύʔπʹbind͢ΔͨΊͷػߏObservableΛDriverʹม ׵͢Δ͜ͱͰɺ 
 ɾ[ϝΠϯεϨουͰߪಡ͞ΕΔ]
 ɾ[ΤϥʔΛग़ྗ͠ͳ͍]
 ɾ[ෳ਺ͷViewʹbindͯ͠΋1ͭͷSubscription͚͕ͩੜ੒ɾڞ༗͞Ε Δ(Hotม׵͞ΕΔ)] 
 ͱ͍ͬͨɺViewʹObservableΛద߹͢ΔͨΊʹඞཁͳॲཧΛཪଆͰߦͬ

    ͯ͘ΕΔɻ
  40. SimpleValidationͷઌ΄ͲͷྫΛDriverͰॻ͘ͱ…. ⏬ asDriver()Λ࣮ߦͨ࣌͠఺ͰHotม׵͞ΕΔͷͰɺ͜ͷ৔߹ShareReplay΋ෆཁʹͳΔɻ

  41. ViewModel (MVVMύλʔϯ) View ViewModel Model <————> ————> <———— Binding UIϩδοΫɺ΋͘͠͸UI

    ͦͷ΋ͷͷఆٛɻiOSͷ ৔߹xib΍StoryboardͰ ఆٛ͞ΕͨUI৘ใ/ CustomViewͳͲ͕֘ ౰ɻViewͷঢ়ଶߋ৽͸ DataBindingػߏΛ௨͡ ͯߦΘΕΔɻ ϓϨθϯςʔγϣϯϩδο ΫΛهड़͢ΔɻViewʹද ࣔ͢΂͖஋ΛBindingͰ ͖Δܗ(Observable/ Driver)ʹͯ͠ެ։͢Δɻ ModelͷมߋΛ؂ࢹ͠ɺ ͦΕʹ߹Θͤͨදࣔϩδο ΫΛهड़͢Δɻ υϝΠϯ૚ɻUIͷ͜ͱ͸ ҙࣝ͠ͳ͍ɻσʔλऔಘ ॲཧɺϏδωεϩδοΫɺ σʔλ͕Ϛοϐϯά͞Ε ͨΤϯςΟςΟͳͲ͔Β ੒ΔɻσʔλΛऔಘ͠ɺ Կ͔͠Βૢ࡞ͨ͠ޙɺσʔ λͷू߹ΛแΜͩ ObservableΛެ։͢ ΔɺͳͲɻ
  42. GithubSignupViewModel2 Viewʹbinding͢ΔDriver͕ެ։͞Ε͍ͯΔɻ͜ͷViewModelͰ ͸subscribe͸࣮ߦ͠ͳ͍ͷͰDisposeBag͸อ͍࣋ͯ͠ͳ͍ɻ

  43. userNameͷvalidate Λ௥ͬͯΈΔ

  44. flatMap: ObservableΛฦ͢ͱɺͦͷObservable͕emit͢ΔཁૉΛ γʔέϯεʹྲྀ͢͜ͱ͕Ͱ͖Δɻ PromiseͷthenͷΠϝʔδʹ͍ۙɻඇಉظॲཧΛॱʹهड़͍ͨ͠ͱ͖ ʹඇৗʹศརɻ্هͷྫͰ͸ɺuserNameͷςΩετʹมߋ͕͋ͬͨ ৔߹ɺ࠷ޙʹemit͞ΕͨTextΛݩʹvalidateUserNameͱ͍͏ඇಉظ ॲཧΛ࣮ߦɺͦͷ݁Ռͷ஋Λemit͢ΔγʔέϯεΛੜ੒͍ͯ͠Δɻ

  45. validateUsername͸ɺStringΛड͚औͬͯObservableΛฦ͢ϝιο υɻ

  46. ValidationResult͸Validateͷ݁ՌΛอ࣋͢ΔEnumܕɻ validateUsername͸ɺStringΛड͚औͬͯObservableΛฦ͢ϝιο υɻͳͷͰɺViewModelͷvalidatedUsernameʹ͸ɺ Observable<ValidationResult>͕֨ೲ͞ΕΔɻ

  47. ੜ੒͞ΕͨvalidationUsernameΛUILabelʹbinding͍ͯ͠Δɻ ex_validationResult͸ɺ͜ͷαϯϓϧͷͨΊʹUILabelΛ֦ுͯ͠࡞Β Εͨؔ਺ɻValidationResultΛUILabelʹbinding͢Δॲཧ͕͜͜ʹ࣮૷ ͞Ε͍ͯΔɻ(textColor, description͸ValidationResultͷEnumʹੜ΍ ͨ͠ϝιου)

  48. signinपΓͷ੍ޚ

  49. ɹdistinctUntilChanged: ྲྀΕͯདྷͨ஋͕લճͱҟͳΔ΋ͷͩͬͨ ͱ͖ͷΈɺγʔέϯεʹ஋Λྲྀ͢ɻͭ·Γɺೖྗ͕͋ͬͯςΩετ͕ มߋ͞Εͯ΋validate݁Ռ͕લճͷೖྗͱಉͩͬͨ͡৔߹ʹ͸γʔέ ϯεʹ஋Λྲྀ͞ͳ͍ɻ

  50. ɹwithLatestFrom: ݩͷObservable(loginTaps)ʹɺࢦఆͨ͠ Obaservableͷγʔέϯεͷ࠷ޙͷཁૉΛ߹੒͢Δɻͭ·Γɺ࠷ޙʹ ೖྗ͞Εͨusernameͱpasswordͷ஋Λ࣋ͬͨObservableʹͳΔɻ

  51. ɹtrackActivity: ͜ͷαϯϓϧͷͨΊʹ࡞ΒΕͨϢʔςΟϦςΟɻ usingΦϖϨʔλΛར༻͠ɺͦͷObservable͕Completed͞ΕΔ·Ͱ ͷؒʹɺΠϯδήʔλΛදࣔ͢Δɻ

  52. ɹusing: Observableͱಉ͡ੜଘظؒΛ࣋ͭϦιʔεͱObservableΛඥ෇͚Δ ͜ͱ͕Ͱ͖Δɻ͜ΕΛར༻͢Ε͹ɺObservable͕ߪಡ͞Ε͸͡Ίͨͱ͖ͷॲ ཧɺͦͯ͠Observableͷߪಡ͕׬ྃͨ͠ͱ͖ͷॲཧΛ࣮૷͢Δ͜ͱ͕Ͱ͖Δɻ ͜͜Ͱ͸ɺ௨৴Λ࣮ߦ͢ΔObservableͷߪಡ͕։࢝͞ΕͨΒ಺෦ͷΧ΢ϯλΛ ΠϯΫϦϝϯτɺObservableͷߪಡ͕׬ྃͨ͠ΒσΫϦϝϯτ͢Δ࣮૷ͱͳͬ ͍ͯΔɻ Χ΢ϯλ͕1Ҏ্ͷͱ͖ʹΠϯδήʔλ͕දࣔ͞ΕΔɻ

  53. Ҏ্ɺRxExampleͷ͝঺հͰͨ͠

  54. GitHubSearchRepositoriesɺ WikipediaImageSearchͳͲࠓճ͸঺հ͠ͳ ͔ͬͨ͞Βʹ࣮ફతͳྫ΋͋ΔͷͰɺڵຯ͋Δ ํ͸ͥͻRxExampleΛಡΈࠐΜͰΈΑ͏ʂ