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

iOS アプリ開発における Redux という可能性

yuuxeno
March 09, 2017

iOS アプリ開発における Redux という可能性

yuuxeno

March 09, 2017
Tweet

More Decks by yuuxeno

Other Decks in Programming

Transcript

  1. MVC

  2. ͍Ζ͍Ζ۩ମతͳ͜ͱΛ஌Γ͗͢ • ࠷ऴతʹɺը໘දࣔʹඞཁͳͷ͸ɺString ΍ UImage ͳͲɺ͔ͳΓݶΒΕͨ΋ͷ • Ͳͷ Model ʹج͍ͮͯɺͲ͏දࣔ͢Δͷ͔ʁ

    ͸ຊ౰ʹ஌͍ͬͯΔඞཁͷ͋Δ͜ͱͳͷͩΖ͏ ͔ʢ A Model ͱ B Model Λ࢖ͬͯɺC Model ͕ಛఆͷঢ়ଶͷ࣌ʹɾɾɾͳͲʣ
  3. ViewController ͕͖ͬ͢Γ͢Δʂ final class CounterViewController: UIViewController { @IBOutlet weak var

    countLabel: UILabel! @IBOutlet weak var incrementButton: UIButton! @IBOutlet weak var decrementButton: UIButton! var viewModel: CounterViewModel! fileprivate let disposeBag = DisposeBag() override func viewDidLoad() { super.viewDidLoad() bind() } }
  4. func bind() Ͱશͯย෇͘ʂ extension CounterViewController { fileprivate func bind() {

    viewModel.count.asObservable() .subscribe(onNext: { [unowned self] in self.countLabel.text = $0 }) .addDisposableTo(disposeBag) incrementButton.rx .tap .bindTo(viewModel.incrementTrigger) .addDisposableTo(disposeBag) decrementButton.rx .tap .bindTo(viewModel.decrementTrigger) .addDisposableTo(disposeBag) } }
  5. ޿͘ࢀর͞ΕΔ Model ʹ͍ͭͯɺ ۩ମతʹҙࣝ͢Δඞཁ͕͋Δͷ͕ͭΒ͍ final class CounterViewModel { let incrementTrigger

    = PublishSubject<Void>() let decrementTrigger = PublishSubject<Void>() let count = Variable("") fileprivate let counter: Counter fileprivate let disposeBag = DisposeBag() // counter ͸ଞͷ ViewModel ɺ΋͘͠͸ Model Ͱ΋࢖ΘΕΔ init(counter: Counter) { self.counter = counter bind() } }
  6. ؾΛ͚ͭͯԼ͍͞ ˑ(ʍωŋ)vŝŒűƅ /* Counter ͸Ͳ͏ͯ͠΋ɺ৭ʑͳͱ͜ΖͰࢀর͞Ε·͢ɻؾΛ͚ͭͯԼ͍͞ ˑ(ʍωŋ)vŝŒűƅ */ final class Counter

    { let count = Variable(0) func increment() { count.value += 1 } func decrement() { count.value -= 1 } }
  7. Redux ͳΒɾɾɾ • View ͸ ͨͩͷ Action Λͨͩඈ͹͚ͩ͢ɺޙ ͷ͜ͱ͸ Store

    ͷ޲͜͏ଆͷੈքͰԿͱ͔͠ ͯ͘ΕΔ • View ͸දࣔʹඞཁͳɺͨͩͷঢ়ଶΛड͚औ Δɻͦͷঢ়ଶ͕Ͳ͏΍ͬͯੜ੒͞Εͨͷ͔͸ɺ Կ΋஌Βͳ͍͍ͯ͘
  8. View override func viewDidLoad() { super.viewDidLoad() // subscribe to state

    changes mainStore.subscribe(self) } func newState(state: AppState) { // when the state changes, the UI is updated to reflect the current state counterLabel.text = "\(mainStore.state.counter)" } // when either button is tapped, an action is dispatched to the store // in order to update the application state @IBAction func downTouch(_ sender: AnyObject) { mainStore.dispatch(CounterActionDecrease()) } @IBAction func upTouch(_ sender: AnyObject) { mainStore.dispatch(CounterActionIncrease()) } https://github.com/ReSwift/CounterExample ৽͍͠ঢ়ଶ "DUJPOΛඈ͹͚ͩ͢
  9. Reducer ͕੔વͱ౷࣏͢Δੈք func handleAction(action: Action, state: AppState?) -> AppState {

    // if no state has been provided, create the default state var state = state ?? AppState() switch action { case _ as CounterActionIncrease: state.counter += 1 case _ as CounterActionDecrease: state.counter -= 1 default: break } return state }
  10. ੍໿ • ঢ়ଶʹର͢Δมߋ͸ɺඞͣ Action Λൃߦ͠ ͯɺ Reducer Ͱߦ͏ • Reducer

    ͸७ਮͳؔ਺Ͱ͋Δ͜ͱʢςετ͕ ॻ͖΍͍͢ʣ • Store ͸̍ͭ
  11. ࠓ·Ͱ final class Counter { var count: Int = 0

    func increment() { count += 1 } func decrement() { count -= 1 } }
  12. Redux /* State */ struct AppState: StateType { var counter:

    Int = 0 } /* Reducer */ struct CounterReducer: Reducer { typealias ReducerStateType = AppState func handleAction(action: Action, state: AppState?) -> AppState { // if no state has been provided, create the default state var state = state ?? AppState() switch action { case _ as CounterActionIncrease: state.counter += 1 case _ as CounterActionDecrease: state.counter -= 1 default: break } return state } }
  13. ՝୊ • ੍໿͕ڧ͍෼ɺ࣮૷ίετ͸ॏͦ͏ • ෭࡞༻ͷஔ͖৔ॴɻMiddleware ? ActionCreator ? AsyncActionCreator ?

    • Middleware ͸Α͘ߟ࣮͑ͯ૷͠ͳ͍ͱΧΦεʹ ͳΓͦ͏ ( redux-saga ͷΑ͏ͳ΋ͷ͕ཉ͍͠ )
  14. ࢀߟ URL • ΞϓϦ։ൃͱঢ়ଶભҠͷ؅ཧ http://ninjinkun.hatenablog.com/entry/2016/02/02/183136 • Read Me · Redux

    http://redux.js.org • ReSwift Reference http://reswift.github.io/ReSwift/master/ • ͳͥReduxΛ࢖͏ͷ͔ https://speakerdeck.com/kuy/nazereduxwoshi-ufalseka • redux-sagaͰඇಉظॲཧͱઓ͏ http://qiita.com/kuy/items/716affc808ebb3e1e8ac • ReduxͷmiddlewareΛੵۃతʹ࢖͍ͬͯ͘ http://qiita.com/kuy/items/ 57c6007f3b8a9b267a8e • redux-sagaͰ෭࡞༻Λίϯτϩʔϧ͢Δ https://speakerdeck.com/kuy/redux-sagadefu- zuo-yong-wokontororusuru