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

RxSwiftと愉快な仲間たち / RxSwift with Units

RxSwiftと愉快な仲間たち / RxSwift with Units

Gotanda.mobile #1 on 2016/05/20

Mihyaeru

May 20, 2016
Tweet

More Decks by Mihyaeru

Other Decks in Technology

Transcript

  1. RxSwiftͱ༇շͳ஥ؒͨͪ
    Gotanda.mobile #1 in Mobile Factory
    on 2016/05/20
    ϛώϟΤϧ@mihyaeru21

    View full-size slide

  2. ͓·͑୭Α
    • ϛώϟΤϧ @mihyaeru21
    • ࠷ۙͷݴޠɿSwift/Kotlin/Perl
    • ·ͩ·ͩϋϯλʔ
    • ϞφυΛཧղ͍ͨ͠

    View full-size slide

  3. Unit
    • Driver
    • Variable
    • ControlEvent/ControlProperty

    View full-size slide

  4. Unit
    • RxCocoa͕ఏڙ͢Δศར͘Μ
    • ෺ཧͱ͔ͷ୯Ґͱͯ͠ͷUnitʹ͔͚͍ͯΔΒ͍͠
    ※RxCocoaʹؚ·ΕΔͱެࣜυΩϡϝϯτʹ͋Δ͚ͲVariable͸RxSwiftʹؚ·ΕΔ

    View full-size slide

  5. Driver
    • Τϥʔ͕ྲྀΕͳ͍
    • observe on ϝΠϯεϨου
    • share͞ΕΔ
    • (ਖ਼֬ʹ͸ shareReplayLatestWhileConnected )

    View full-size slide

  6. Driver
    • ϞσϧͱUIͷؒʹೖͬͨΓ
    • UIͱUIΛܨ͍ͩΓ
    • ϋʔυ΢ΣΞͷυϥΠόιϑτ΢ΣΞతͳͱ͜Ζ͔Β

    ໊෇͚ΒΕͨ

    View full-size slide

  7. Driver
    fetchUser()
    .map { $0.name }
    .observeOn(MainScheduler.instance)
    .bindTo(label.rx_text)

    View full-size slide

  8. Driver
    fetchUser()
    .map { $0.name }
    .observeOn(MainScheduler.instance)
    .bindTo(label.rx_text)
    όοΫάϥ΢ϯυͰσʔλΛऔͬͯ͘Δ
    UIͷߋ৽͸ϝΠϯεϨουඞਢ
    ϥϕϧͷςΩετʹόΠϯυ͢Δ

    View full-size slide

  9. Driver
    fetchUser()
    .map { $0.name }
    .asDriver(onErrorJustReturn: "")
    .drive(label.rx_text)

    View full-size slide

  10. Driver
    fetchUser()
    .map { $0.name }
    .asDriver(onErrorJustReturn: "")
    .drive(label.rx_text)
    Driverʹม׵
    ϥϕϧͷςΩετʹόΠϯυ͢Δ

    View full-size slide

  11. Driver
    let userName = fetchUser()
    .map { $0.name }
    .observeOn(MainScheduler.instance)
    userName.bindTo(label.rx_text)
    userName.bindTo(otherLabel.rx_text)

    View full-size slide

  12. Driver
    let userName = fetchUser()
    .map { $0.name }
    .observeOn(MainScheduler.instance)
    .share()
    userName.bindTo(label.rx_text)
    userName.bindTo(otherLabel.rx_text)
    share()͠ͳ͍ͱ2ճൃՐ͢Δ

    View full-size slide

  13. Driver
    let userName = fetchUser()
    .map { $0.name }
    .asDriver(onErrorJustReturn: "")
    userName.drive(label.rx_text)
    userName.drive(otherLabel.rx_text)

    View full-size slide

  14. Driver
    let userName = fetchUser()
    .map { $0.name }
    .asDriver(onErrorJustReturn: "")
    userName.drive(label.rx_text)
    userName.drive(otherLabel.rx_text)
    share()తͳ͜ͱΛ͍ͯ͠Δ͔ΒൃՐ͸1ճͷΈ

    View full-size slide

  15. Driver
    • ʮͦΕɺObservableͰ΋Ͱ͖ͯΔ͡ΌΜʯ
    • ͸͍

    View full-size slide

  16. Driver
    • UIͱܨ͙ҙਤΛ໌֬ʹ͢Δ
    • ҆શ౓ΛߴΊΔ
    • Driverʹม׵ޙ͸observeεϨουͷมߋ͸Ͱ͖ͳ͍
    • share()ͯ͘͠ΕΔ

    View full-size slide

  17. Variable
    • Τϥʔ͕ྲྀΕͳ͍
    • share͞ΕΔ

    View full-size slide

  18. Variable
    • BehaviorSubjectͷWrapper
    • valueϓϩύςΟΛ௨ͯ͠࠷৽ͷ஋ʹΞΫηεͰ͖Δ
    • valueϓϩύςΟ΁ͷ୅ೖ͸on(.Next)ͱͯ͠ྲྀΕΔ
    • ObservableʹͳΔ͚ͲObserverʹ͸ͳΒͳ͍
    • ஋Λྲྀͨ͢Ίʹ͸valueΛඞͣܦ༝͢Δ

    View full-size slide

  19. Variable
    let variable = Variable(0)
    variable
    .asDriver()
    .map { "value: \($0)" }
    .drive(label.rx_text)
    variable.value = 99
    print("variable.value = \(variable.value)")

    View full-size slide

  20. Variable
    let variable = Variable(0)
    variable
    .asDriver()
    .map { "value: \($0)" }
    .drive(label.rx_text)
    variable.value = 99
    print("variable.value = \(variable.value)")
    label.text͕”value: 99”ʹͳΔ
    Variable͸ΤϥʔΛྲྀ͞ͳ͍͔Β
    Ҿ਺ແ͠൛ͷasDriver()͕͋Δ
    ಉظతʹ஋ΛऔಘͰ͖Δ

    View full-size slide

  21. ControlEvent
    ControlProperty

    View full-size slide

  22. ControlEvent/ControlProperty
    • Τϥʔ͕ྲྀΕͳ͍
    • subscribe on ϝΠϯεϨου
    • observe on ϝΠϯεϨου
    • share͞ΕΔ

    View full-size slide

  23. ControlEvent/ControlProperty
    • UIύʔπͷΠϕϯτΛRxͱͭͳ͗ࠐΈ΍͘͢
    • ControlEvent
    • Observable
    • ControlProperty
    • Observable / Observer
    • Observer΋͋Δ

    View full-size slide

  24. ControlEvent/ControlProperty
    textField.rx_text
    .asDriver()
    .drive(label.rx_text)
    ControlProperty
    Observer
    ControlProperty͸ΤϥʔΛྲྀ͞ͳ͍͔Β
    Ҿ਺ແ͠൛ͷasDriver()͕͋Δ

    View full-size slide

  25. ControlEvent/ControlProperty
    textField.rx_text
    .bindTo(label.rx_text)
    ControlPropertyͷΠϕϯτ͸ϝΠϯεϨουͰྲྀΕΔ

    View full-size slide

  26. ControlEvent/ControlProperty
    button.rx_tap
    .asDriver()
    .map { "tapped!" }
    .drive(textField.rx_text)
    ControlEvent΋ΤϥʔΛྲྀ͞ͳ͍͔Β
    Ҿ਺ແ͠൛ͷasDriver()͕͋Δ
    ControlEvent
    ControlProperty

    View full-size slide

  27. ControlEvent/ControlProperty
    • Cocoaͷ͍ΖΜͳ΍ͭʹ rx_* ͕ੜ͍͑ͯͯΖ͍Ζศར
    • https://github.com/ReactiveX/RxSwift/blob/master/
    Documentation/API.md

    View full-size slide

  28. ౜ಥͳ·ͱΊ
    • ศརػೳͨͪ
    • Driver
    • Variable
    • ControlEvent/ControlProperty
    • ͳͯ͘΋ࢮͳͳ͍͚Ͳ͋Δͱ஍ຯʹศར
    • ஍ຯʹ࢖ͬͯ͜

    View full-size slide