ReactorKit Meetup Japan at Wantedly, Inc. 日本初 ReactorKit のコミュニティーイベント開催。作者来日で特別講演 https://wantedly.connpass.com/event/90261/
try! ReactorKit2018.06.28 - ReactorKit Meetup Japan at Wantedly, Inc.Satoshi Hachiya - @jpmartha_jp
View Slide
Satoshi Hachiya - @jpmartha_jpworking at
Welcome to Japan!
I've met Suyeol Jeon before
try! Swift NYC 2017 (Sep. 2017)
WWDC 2018 (Jun. 2018)
And then, I met ReactorKit
ReactorKit• I just started learning RxSwift and ReactorKit• The documents are easy to understand• The examples are abundant#Anytime you ready!
ReactorKit Exampleshttps://github.com/ReactorKit/ReactorKit#examples
ReactorKitΛֶͿγϦʔζ (Japanese)https://qiita.com/yusuga/items/e793963ff51ee493497a
!
try! ReactorKit
Installing ReactorKit• Use CocoaPods• Create reactors• ...
Using ReactorKitViewController - ReactorViewController - ReactorCell - Reactor...
Reactor
ActionʢΞΫγϣϯʣenum Action {case increase // ૿Ճcase decrease // ݮগ}
MutationʢมԽʣenum Mutation {case increaseValue // Λ૿͢case decreaseValue // ΛݮΒ͢...}
Stateʢঢ়ଶʣstruct State {var value: Int // ...}
initialStateʢঢ়ଶͷॳظʣlet initialState: Stateinit() {self.initialState = State(value: 0, // 0 ͔Β͡·Δ...)}
View
ViewControllerfunc bind(reactor: CounterViewReactor) {increaseButton.rx.tap.map { Reactor.Action.increase }.bind(to: reactor.action).disposed(by: disposeBag)...}
Easy
Introducing failure casesfor beginners
Run!
... No action
Need to set `reactor` property
viewController.reactor = MyViewReactor()func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {let viewController = self.window?.rootViewController as! MyViewControllerviewController.reactor = MyViewReactor()return true}
Other caseusing UICollectionView
Runtime Error
Runtime Errorclass MyViewController: UIViewController, StoryboardView {...func bind(reactor: MyViewReactor) {collectionView.rx.contentOffset.map { _ in Reactor.Action.increase }.bind(to: reactor.action).disposed(by: disposeBag)}...}
https://github.com/ReactiveX/RxSwift/issues/1587
+ UICollectionViewDelegateclass PageMenuViewController: UIViewController, UICollectionViewDelegate, StoryboardView {...func bind(reactor: MyViewReactor) {collectionView.rx.contentOffset.map { _ in Reactor.Action.increase }.bind(to: reactor.action).disposed(by: disposeBag)}...}
It worked!
Review
Basic Concept
Unidirectional Data Flow
View➡Actionfunc bind(reactor: CounterViewReactor) {increaseButton.rx.tap.map { Reactor.Action.increase }.bind(to: reactor.action).disposed(by: disposeBag)...}
Action➡Mutationfunc mutate(action: Action) -> Observable {switch action {case .increase:return Observable.concat([...,Observable.just(Mutation.increaseValue).delay(0.5, scheduler: MainScheduler.instance),...,])case .decrease:...}}
Mutation Statefunc reduce(state: State, mutation: Mutation) -> State {var state = stateswitch mutation {case .increaseValue:state.value += 1...return state}
State➡Viewfunc bind(reactor: CounterViewReactor) {...reactor.state.map { $0.value }.distinctUntilChanged().map { "\($0)" }.bind(to: valueLabel.rx.text).disposed(by: disposeBag)...}
Good readability of the code• ReactorKit makes it easier to...• separate the responsibílities• manage the state of the views• manage the data flow
Thanks!