Slide 1

Slide 1 text

Flux with RxSwift AbemaTV Developer Conference 2016 Yuji Hato

Slide 2

Slide 2 text

About me Yuji Hato CyberAgent, Inc. / AbemaTV, Inc. dekatotoro @dekatotoro Contributed services

Slide 3

Slide 3 text

What is Flux?

Slide 4

Slide 4 text

What is Flux? IUUQTGBDFCPPLHJUIVCJPqVYEPDTPWFSWJFXIUNM “Data in a Flux application flows in a single direction”

Slide 5

Slide 5 text

What is Flux? IUUQTHJUIVCDPNGBDFCPPLqVY

Slide 6

Slide 6 text

What is Flux? IUUQTHJUIVCDPNGBDFCPPLqVY

Slide 7

Slide 7 text

What is Flux? IUUQTHJUIVCDPNGBDFCPPLqVY Observer ύλʔϯ !

Slide 8

Slide 8 text

Why Flux?

Slide 9

Slide 9 text

Why Flux? ࡢࠓͷΞϓϦ։ൃ͸ෳࡶԽ ͷҰ్Λḷ͓ͬͯΓঢ়ଶ؅ཧ ͕େม

Slide 10

Slide 10 text

Why Flux? AbemaTVͷঢ়ଶ؅ཧ: Cast ΦϯσϚϯυ ՝ۚ CM Filler ը࣭ ࢹௌ༧໿ etc… ൪૊ Fresh ίϝϯτ ࢹௌ਺ FullScreen Feed

Slide 11

Slide 11 text

Why Flux? ͭΒ͍…

Slide 12

Slide 12 text

Why Flux? UIૢ࡞ɾ࣌ؒʹ൐͏ෳࡶͳ ঢ়ଶભҠΛ෼͔Γ΍͍ͨ͘͢͠

Slide 13

Slide 13 text

Why Flux? Viewؒͷґଘؔ܎ΛݮΒ͍ͨ͠

Slide 14

Slide 14 text

MVVM MVC DDD Flux Clean Architecture MVP Why Flux? Flux͕޲͍ͯͦ͏…?

Slide 15

Slide 15 text

Flux with RxSwift

Slide 16

Slide 16 text

Flux with RxSwift Action Dispatcher User Interactions API DB ViewController View Store Events Events

Slide 17

Slide 17 text

Flux with RxSwift Event StreamͰܨ͍͡Ό͓͏ͥ

Slide 18

Slide 18 text

Flux with RxSwift Action Dispatcher User Interactions API DB ViewController View Store Events Events

Slide 19

Slide 19 text

Flux with RxSwift Dispatcher ViewController View Store Events Events Events Event͸̎छྨͷHot ObservableΛ࢖͏

Slide 20

Slide 20 text

Flux with RxSwift PublishSubject Ұ੾Ωϟογϡ͠ͳ͍Subject Events

Slide 21

Slide 21 text

Flux with RxSwift Variable ௚ۙͷ஋Λ͚̍ͭͩΩϟογϡ͢ΔSubject ※BehaviorSubjectͷwrapper Events

Slide 22

Slide 22 text

Dispatcher

Slide 23

Slide 23 text

Dispatcher Action Dispatcher User Interactions API DB ViewController View Store Events Events

Slide 24

Slide 24 text

Dispatcher Dispatcherͷϑϩʔ Dispatcher Store Events Events Action Dispatcher Store Store Events Events Action Action

Slide 25

Slide 25 text

Dispatcher Action͕dispatch͢Δ Dispatcher Store Store Events Events Action Action on subscribe

Slide 26

Slide 26 text

Dispatcher dispatch͞ΕͨΒStore΁௨஌ Dispatcher Store Store Events Events Action Action on subscribe

Slide 27

Slide 27 text

Dispatcher class DispatchSubject: ObservableType, ObserverType { typealias E = Element fileprivate let subject = PublishSubject() func dispatch(_ value: E) { on(.next(value)) } func on(_ event: Event) { subject.on(event) } func subscribe(_ observer: O) -> Disposable where O.E == E { return subject.subscribe(observer) } } Dispatcher༻ͷDispatchSubject

Slide 28

Slide 28 text

Dispatcher class DispatchSubject: ObservableType, ObserverType { typealias E = Element fileprivate let subject = PublishSubject() func dispatch(_ value: E) { on(.next(value)) } func on(_ event: Event) { subject.on(event) } func subscribe(_ observer: O) -> Disposable where O.E == E { return subject.subscribe(observer) } } PublishSubjectͷWrapper

Slide 29

Slide 29 text

Dispatcher class DispatchSubject: ObservableType, ObserverType { typealias E = Element fileprivate let subject = PublishSubject() func dispatch(_ value: E) { on(.next(value)) } func on(_ event: Event) { subject.on(event) } func subscribe(_ observer: O) -> Disposable where O.E == E { return subject.subscribe(observer) } } dispatch͢ΔͱeventΛൃߦ͠·͢

Slide 30

Slide 30 text

Dispatcher class SomeDispatcher { static let shared = SomeDispatcher() let loading = DispatchSubject() let error = DispatchSubject() let someModel = DispatchSubject() … } DispatcherΫϥε

Slide 31

Slide 31 text

Dispatcher class SomeDispatcher { static let shared = SomeDispatcher() let loading = DispatchSubject() let error = DispatchSubject() let someModel = DispatchSubject() … } ActionTypeͷ୅ΘΓʹDispatchSubjectΛෳ਺༻ҙ ※DispatcherΫϥε΋༻్͝ͱʹ෼͚ͯ·͢

Slide 32

Slide 32 text

Dispatcher func someAction(value: Bool) { … dispatcher.loading.dispatch(value) … } Action͕dispatch

Slide 33

Slide 33 text

Action

Slide 34

Slide 34 text

Action Action Dispatcher User Interactions API DB ViewController View Store Events Events

Slide 35

Slide 35 text

Action Actionͷϑϩʔ Action Action Dispatcher User Interactions API DB ViewController View Action Action Dispatcher API DB ViewController View Device User Interactions

Slide 36

Slide 36 text

Action View͔ΒActionΛ࣮ߦ Action Action Dispatcher User Interactions API DB ViewController View Action Action Dispatcher API DB ViewController View Device User Interactions Action Action Dispatcher User Interactions API DB ViewController View Action Action Action Dispatcher API DB ViewController View Device User Interactions

Slide 37

Slide 37 text

Action ඞཁͳσʔλΛऔಘ͢Δ Action Action Dispatcher User Interactions API DB ViewController View Action Action Action Dispatcher API DB ViewController View Device User Interactions

Slide 38

Slide 38 text

Action UI, Web, DB, DevicesΛ֎෦IFͱͯ͠ଊ͑Δ IUUQTUIMJHIUDPNCMPHVODMFCPCUIFDMFBOBSDIJUFDUVSFIUNM

Slide 39

Slide 39 text

Action σʔλͷऔಘޙDispatcher΁ྲྀ͢ Action Action Dispatcher User Interactions API DB ViewController View Action Action Action Dispatcher API DB ViewController View Device User Interactions

Slide 40

Slide 40 text

Action func someAction(query: String) { dispatcher.loading.dispatch(true) API.getSome(with: query) .do(onError: { [unowned self] error in self.dispatcher.error.dispatch(error) self.dispatcher.loading.dispatch(false) }) .do(onCompleted: { [unowned self] error in self.dispatcher.loading.dispatch(false) }) .subscribe(onNext: { [unowned self] response in let someModel = SomeModel.make(from: response) self.dispatcher.someModel.dispatch(someModel) }) .addDisposableTo(disposeBag) } APIΛ࣮ߦ͢Δྫ

Slide 41

Slide 41 text

Action func someAction(query: String) { dispatcher.loading.dispatch(true) API.getSome(with: query) .do(onError: { [unowned self] error in self.dispatcher.error.dispatch(error) self.dispatcher.loading.dispatch(false) }) .do(onCompleted: { [unowned self] error in self.dispatcher.loading.dispatch(false) }) .subscribe(onNext: { [unowned self] response in let someModel = SomeModel.make(from: response) self.dispatcher.someModel.dispatch(someModel) }) .addDisposableTo(disposeBag) } ϩʔσΟϯάͯ͠API࣮ߦ

Slide 42

Slide 42 text

Action func someAction(query: String) { dispatcher.loading.dispatch(true) API.getSome(with: query) .do(onError: { [unowned self] error in self.dispatcher.error.dispatch(error) self.dispatcher.loading.dispatch(false) }) .do(onCompleted: { [unowned self] error in self.dispatcher.loading.dispatch(false) }) .subscribe(onNext: { [unowned self] response in let someModel = SomeModel.make(from: response) self.dispatcher.someModel.dispatch(someModel) }) .addDisposableTo(disposeBag) } Τϥʔͷ࣌

Slide 43

Slide 43 text

Action func someAction(query: String) { dispatcher.loading.dispatch(true) API.getSome(with: query) .do(onError: { [unowned self] error in self.dispatcher.error.dispatch(error) self.dispatcher.loading.dispatch(false) }) .do(onCompleted: { [unowned self] error in self.dispatcher.loading.dispatch(false) }) .subscribe(onNext: { [unowned self] response in let someModel = SomeModel.make(from: response) self.dispatcher.someModel.dispatch(someModel) }) .addDisposableTo(disposeBag) } Ϩεϙϯε͕ฦ͖ͬͯͨ࣌

Slide 44

Slide 44 text

Action func someAction(query: String) { dispatcher.loading.dispatch(true) API.getSome(with: query) .do(onError: { [unowned self] error in self.dispatcher.error.dispatch(error) self.dispatcher.loading.dispatch(false) }) .do(onCompleted: { [unowned self] error in self.dispatcher.loading.dispatch(false) }) .subscribe(onNext: { [unowned self] response in let someModel = SomeModel.make(from: response) self.dispatcher.someModel.dispatch(someModel) }) .addDisposableTo(disposeBag) } ࣮ߦ͕׬ྃͨ࣌͠

Slide 45

Slide 45 text

Store

Slide 46

Slide 46 text

Store Action Dispatcher User Interactions API DB ViewController View Store Events Events

Slide 47

Slide 47 text

Store Storeͷϑϩʔ Dispatcher Store Store ViewController View Events Events Events

Slide 48

Slide 48 text

Store DispatcherͷeventΛObserve͢Δ Dispatcher Store Store ViewController View Events Events Events subscribe on on subscribe

Slide 49

Slide 49 text

Store DispatcherͷΠϕϯτ͕ྲྀΕͯ͘Δ Store Store ViewController View Events Events Events subscribe on on subscribe Dispatcher

Slide 50

Slide 50 text

Store DispatcherͷΠϕϯτΛݕ஌ͨ͠ΒσʔλΛߋ৽ Store Store ViewController View Events Events Events subscribe on on subscribe Dispatcher

Slide 51

Slide 51 text

Store σʔλΛߋ৽ͨ͠ΒView΁௨஌ Store Store ViewController View Events Events Events subscribe subscribe Dispatcher on on

Slide 52

Slide 52 text

Flux: Store class Store { let disposeBag = DisposeBag() func bind(_ observable: O, _ param: Variable) where O: ObservableType, E == O.E { observable.bindTo(param).addDisposableTo(disposeBag) } func bind(_ observable: O, _ param: PublishSubject) where O: ObservableType, E == O.E { observable.bindTo(param).addDisposableTo(disposeBag) } } Store਌Ϋϥε

Slide 53

Slide 53 text

Flux: Store class Store { let disposeBag = DisposeBag() func bind(_ observable: O, _ param: Variable) where O: ObservableType, E == O.E { observable.bindTo(param).addDisposableTo(disposeBag) } func bind(_ observable: O, _ param: PublishSubject) where O: ObservableType, E == O.E { observable.bindTo(param).addDisposableTo(disposeBag) } } Variable༻ͷbind

Slide 54

Slide 54 text

Flux: Store class Store { let disposeBag = DisposeBag() func bind(_ observable: O, _ param: Variable) where O: ObservableType, E == O.E { observable.bindTo(param).addDisposableTo(disposeBag) } func bind(_ observable: O, _ param: PublishSubject) where O: ObservableType, E == O.E { observable.bindTo(param).addDisposableTo(disposeBag) } } PublishSubject༻ͷbind

Slide 55

Slide 55 text

Flux: Store class SomeStore: Store { static let shared = SomeStore() let loading = Variable(false) let error = PublishSubject() let someModel = Variable(SomeModel()) init(dispatcher: SomeDispatcher = .shared) { super.init() bind(dispatcher.loading, loading) bind(dispatcher.error, error) bind(dispatcher.someModel, someModel) } } StoreΫϥε

Slide 56

Slide 56 text

Flux: Store class SomeStore: Store { static let shared = SomeStore() let loading = Variable(false) let error = PublishSubject() let someModel = Variable(SomeModel()) init(dispatcher: SomeDispatcher = .shared) { super.init() bind(dispatcher.loading, loading) bind(dispatcher.error, error) bind(dispatcher.someModel, someModel) } } StoreΫϥε͸Singleton

Slide 57

Slide 57 text

Flux: Store class SomeStore: Store { static let shared = SomeStore() let loading = Variable(false) let error = PublishSubject() let someModel = Variable(SomeModel()) init(dispatcher: SomeDispatcher = .shared) { super.init() bind(dispatcher.loading, loading) bind(dispatcher.error, error) bind(dispatcher.someModel, someModel) } } Storeͷproperty͸VariableͱPublishSubject

Slide 58

Slide 58 text

Flux: Store class SomeStore: Store { static let shared = SomeStore() let loading = Variable(false) let error = PublishSubject() let someModel = Variable(SomeModel()) init(dispatcher: SomeDispatcher = .shared) { super.init() bind(dispatcher.loading, loading) bind(dispatcher.error, error) bind(dispatcher.someModel, someModel) } } initͰdispatcherͱbind

Slide 59

Slide 59 text

View

Slide 60

Slide 60 text

View Action Dispatcher User Interactions API DB ViewController View Store Events Events

Slide 61

Slide 61 text

View Viewͷϑϩʔ Dispatcher Events Store Store ViewController View Events Action Action Action User Interactions ViewController View Action

Slide 62

Slide 62 text

View StoreͱUIͷeventΛObserve͢Δ Dispatcher Events Store Store ViewController View Events subscribe Action Action User Interactions ViewController View Action

Slide 63

Slide 63 text

View StoreͷΠϕϯτ͕ྲྀΕͯ͘Δ Dispatcher Events Store Store ViewController View Events on Action Action User Interactions ViewController View Action

Slide 64

Slide 64 text

View StoreͷΠϕϯτΛݕ஌ͨ͠ΒViewΛߋ৽ Dispatcher Events Store Store ViewController View Events on Action Action User Interactions ViewController View Action

Slide 65

Slide 65 text

View UIͷeventΛτϦΨʔʹActionΛ࣮ߦ Dispatcher Events Store Store ViewController View Events Action Action User Interactions ViewController View Action

Slide 66

Slide 66 text

View store.loading.asObservable() .distinctUntilChanged() .observeOn(MainScheduler.instance) .subscribe(onNext: { [unowned self] loading in self.loadingView.hidden(!loading) }) .addDisposableTo(rx_disposeBag) store.error .subscribe(onNext: { error in ErrorAction.show(.apiError(error)) }) .addDisposableTo(rx_disposeBag) store.someModel.asObservable() .map { $0.dataSource } .observeOn(MainScheduler.instance) .subscribe(onNext: { [unowned self] dataSource in self.dataSource.value = dataSource self.tableView?.reloadData() }) .addDisposableTo(rx_disposeBag) Storeͷobserve

Slide 67

Slide 67 text

View store.loading.asObservable() .distinctUntilChanged() .observeOn(MainScheduler.instance) .subscribe(onNext: { [unowned self] loading in self.loadingView.hidden(!loading) }) .addDisposableTo(rx_disposeBag) store.error .subscribe(onNext: { error in ErrorAction.show(.apiError(error)) }) .addDisposableTo(rx_disposeBag) store.someModel.asObservable() .map { $0.dataSource } .observeOn(MainScheduler.instance) .subscribe(onNext: { [unowned self] dataSource in self.dataSource.value = dataSource self.tableView?.reloadData() }) .addDisposableTo(rx_disposeBag) StoreͷloadingΛobserve

Slide 68

Slide 68 text

View store.loading.asObservable() .distinctUntilChanged() .observeOn(MainScheduler.instance) .subscribe(onNext: { [unowned self] loading in self.loadingView.hidden(!loading) }) .addDisposableTo(rx_disposeBag) store.error .subscribe(onNext: { error in ErrorAction.show(.apiError(error)) }) .addDisposableTo(rx_disposeBag) store.someModel.asObservable() .map { $0.dataSource } .observeOn(MainScheduler.instance) .subscribe(onNext: { [unowned self] dataSource in self.dataSource.value = dataSource self.tableView?.reloadData() }) .addDisposableTo(rx_disposeBag) loadingViewͷදࣔ/ඇදࣔ

Slide 69

Slide 69 text

View store.loading.asObservable() .distinctUntilChanged() .observeOn(MainScheduler.instance) .subscribe(onNext: { [unowned self] loading in self.loadingView.hidden(!loading) }) .addDisposableTo(rx_disposeBag) store.error .subscribe(onNext: { error in ErrorAction.show(.apiError(error)) }) .addDisposableTo(rx_disposeBag) store.someModel.asObservable() .map { $0.dataSource } .observeOn(MainScheduler.instance) .subscribe(onNext: { [unowned self] dataSource in self.dataSource.value = dataSource self.tableView?.reloadData() }) .addDisposableTo(rx_disposeBag) StoreͷerrorΛobserve

Slide 70

Slide 70 text

View store.loading.asObservable() .distinctUntilChanged() .observeOn(MainScheduler.instance) .subscribe(onNext: { [unowned self] loading in self.loadingView.hidden(!loading) }) .addDisposableTo(rx_disposeBag) store.error .subscribe(onNext: { error in ErrorAction.show(.apiError(error)) }) .addDisposableTo(rx_disposeBag) store.someModel.asObservable() .map { $0.dataSource } .observeOn(MainScheduler.instance) .subscribe(onNext: { [unowned self] dataSource in self.dataSource.value = dataSource self.tableView?.reloadData() }) .addDisposableTo(rx_disposeBag) ErrorActionΛ࣮ߦ

Slide 71

Slide 71 text

View store.loading.asObservable() .distinctUntilChanged() .observeOn(MainScheduler.instance) .subscribe(onNext: { [unowned self] loading in self.loadingView.hidden(!loading) }) .addDisposableTo(rx_disposeBag) store.error .subscribe(onNext: { error in ErrorAction.show(.apiError(error)) }) .addDisposableTo(rx_disposeBag) store.someModel.asObservable() .map { $0.dataSource } .observeOn(MainScheduler.instance) .subscribe(onNext: { [unowned self] dataSource in self.dataSource.value = dataSource self.tableView?.reloadData() }) .addDisposableTo(rx_disposeBag) StoreͷsomeModelΛobserve

Slide 72

Slide 72 text

View store.loading.asObservable() .distinctUntilChanged() .observeOn(MainScheduler.instance) .subscribe(onNext: { [unowned self] loading in self.loadingView.hidden(!loading) }) .addDisposableTo(rx_disposeBag) store.error .subscribe(onNext: { error in ErrorAction.show(.apiError(error)) }) .addDisposableTo(rx_disposeBag) store.someModel.asObservable() .map { $0.dataSource } .observeOn(MainScheduler.instance) .subscribe(onNext: { [unowned self] dataSource in self.dataSource.value = dataSource self.tableView?.reloadData() }) .addDisposableTo(rx_disposeBag) tableViewΛreload

Slide 73

Slide 73 text

View searchBar.rx.text.asDriver() .throttle(0.3) .distinctUntilChanged() .drive(onNext: { query in SomeAction.someAction(query: query) }) .addDisposableTo(rx_disposeBag) UIͷeventΛobserve

Slide 74

Slide 74 text

View searchBar.rx.text.asDriver() .throttle(0.3) .distinctUntilChanged() .drive(onNext: { query in SomeAction.someAction(query: query) }) .addDisposableTo(rx_disposeBag) searchBarͷtextೖྗΛobserve

Slide 75

Slide 75 text

View searchBar.rx.text.asDriver() .throttle(0.3) .distinctUntilChanged() .drive(onNext: { query in SomeAction.someAction(query: query) }) .addDisposableTo(rx_disposeBag) ೖྗ͞Εͨจࣈʹมߋ͕͋ΔຖʹActionΛ࣮ߦ

Slide 76

Slide 76 text

Flux with RxSwift Action Dispatcher User Interactions API DB ViewController View Store Events Events

Slide 77

Slide 77 text

Ұप͠·ͨ͠ʂ Flux with RxSwift

Slide 78

Slide 78 text

Proposals

Slide 79

Slide 79 text

Proposals • Dispatcher͸Ұͭʹ͍ͨ͠ • Store͸SingletonͰͳ͘ɺద੾ͳϥΠϑαΠ Ϋϧʹ͍ͨ͠ • StoreΛread-onlyʹ͍ͨ͠

Slide 80

Slide 80 text

Conclusion

Slide 81

Slide 81 text

Conclusion Pros • ෳ਺ͷViewController, ViewΛ࢖ͬͯෳࡶͳঢ়ଶ ؅ཧΛ͢ΔΞϓϦέʔγϣϯʹ޲͍͍ͯΔ • ։ൃऀͷ࣮૷͕౷Ұ͞Ε΍͍͢ • Viewؒͷґଘؔ܎͕ݮΔ

Slide 82

Slide 82 text

Conclusion Cons • SingletonͰूத؅ཧͳͷͰɺ޷͖ݏ͍͋Δ͔΋ • ׳ΕΔ·Ͱগ͔͔͠Δ • ࣮૷͕৑௕ʹײ͡Δ…?

Slide 83

Slide 83 text

Flux͸࿮૊ΈͳͷͰɺΑΓྑ͘͢ΔͨΊʹ νʔϜͰTrial&Errorͯ͠·͢ Flux with RxSwift

Slide 84

Slide 84 text

Thank you ࢀߟࢿྉ https://facebook.github.io/flux/ https://github.com/facebook/flux https://github.com/thoughtbot/Delta https://github.com/yonekawa/SwiftFlux https://speakerdeck.com/ogaclejapan/flux-de-relax

Slide 85

Slide 85 text

https://github.com/dekatotoro/FluxWithRxSwiftSample Flux with RxSwift Sample