Share
Embed
iOS アプリ開発における Redux という可能性
yuuxeno
March 09, 2017
Transcript
45 slides
-
ࣗݾհ • @yuuxeno • iOS ΤϯδχΞ • ʮFX Simulatorʯͱ͍͏ iOS
ΞϓϦΛϦϦʔ ε • ڵຯɾɾɾRedux, Unity -
͍Ζ͍Ζ۩ମతͳ͜ͱΛΓ͗͢ • ࠷ऴతʹɺը໘දࣔʹඞཁͳͷɺString UImage ͳͲɺ͔ͳΓݶΒΕͨͷ • Ͳͷ Model ʹج͍ͮͯɺͲ͏දࣔ͢Δͷ͔ʁ
ຊʹ͍ͬͯΔඞཁͷ͋Δ͜ͱͳͷͩΖ͏ ͔ʢ A Model ͱ B Model ΛͬͯɺC Model ͕ಛఆͷঢ়ଶͷ࣌ʹɾɾɾͳͲʣ -
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() } } -
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) } } -
໌֬ͳ • View ɺࣗͰ͔͠Ͱ͖ͳ͍͜ͱʹूத • View ɺࣗͷঢ়ଶมԽΛ ViewModel ʹ ͑Δ͚ͩɺͦͷઌͷ͜ͱΔඞཁͳ͍ʢͲ
ͷ Model ΛɺͲ͏͍͏෩ʹͬͯɺͲͷλΠ ϛϯάͰɾɾɾͳͲʣ -
View ͰͷΈ͕ɺগͳ͘ͳͬͨʂ • ϓϨθϯςʔγϣϯϩδοΫͷେ෦Λؙ͛Ͱ͖ Δ • View ͷঢ়ଶΛɺViewModel ʹ͑Δ͚ͩɻ͋ͱͷ ͜ͱɺViewModel
ͷ͜͏ଆͷੈքͰɺԿͱ͔͠ ͯ͘ΕΔɻ • දࣔͷͨΊͷ۩ମతͳ͜ͱΔඞཁ͕ͳ͍ͷͰؾ ָ͕ -
͘ࢀর͞ΕΔ 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() } } -
Δඞཁ͕͋Δ͜ͱ࠷খݶʹ • DI ύλʔϯ -> ۩ମతʹͦͷΦϒδΣΫτ͕Ͳ ͏ੜ͞ΕΔͷ͔ɺΔඞཁΛͳͨ͘͠ • Facade ύλʔϯ
-> ༻ऀ͕۩ମతͳɺҰ࿈ ͷॲཧͷํ๏ΛΔඞཁΛͳͨ͘͠ -
ؾΛ͚ͭͯԼ͍͞ ˑ(ʍωŋ)vŝŒűƅ /* Counter Ͳ͏ͯ͠ɺ৭ʑͳͱ͜ΖͰࢀর͞Ε·͢ɻؾΛ͚ͭͯԼ͍͞ ˑ(ʍωŋ)vŝŒűƅ */ final class Counter
{ let count = Variable(0) func increment() { count.value += 1 } func decrement() { count.value -= 1 } } -
͋ΔهࣄΛݟ͚ͭΔ • ΞϓϦ։ൃͱঢ়ଶભҠͷཧɹɹɹɹɹɹɹɹɹɹɹɹɹɹ http://ninjinkun.hatenablog.com/entry/2016/02/02/183136 • ΞϓϦେ͖ͳγϯάϧτϯ • DIͰΠϯελϯεԽΛ੍ޚ͢Δ͜ͱͰ͖Δ͕ɺ݁ہ෦తʹ γϯάϧτϯͰ͋Δɻ •
์ਤʹ࡞ΒΕΔγϯάϧτϯߋ৽ͷͨΊͷنΛ࣋ͨͳ͍ɻ • ͋ΔఔෳࡶԽͨ͠ΫϥΠΞϯταΠυ։ൃͱɺঢ়ଶભҠΛ͍ ͔ʹཧ͢Δ͔ -
Redux ͳΒɾɾɾ • View ͨͩͷ Action Λͨͩඈ͚ͩ͢ɺޙ ͷ͜ͱ Store
ͷ͜͏ଆͷੈքͰԿͱ͔͠ ͯ͘ΕΔ • View දࣔʹඞཁͳɺͨͩͷঢ়ଶΛड͚औ Δɻͦͷঢ়ଶ͕Ͳ͏ͬͯੜ͞Εͨͷ͔ɺ ԿΒͳ͍͍ͯ͘ -
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Λඈ͚ͩ͢ -
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 } -
੍ • ঢ়ଶʹର͢Δมߋɺඞͣ Action Λൃߦ͠ ͯɺ Reducer Ͱߦ͏ • Reducer
७ਮͳؔͰ͋Δ͜ͱʢςετ͕ ॻ͖͍͢ʣ • Store ̍ͭ -
ࠓ·Ͱ final class Counter { var count: Int = 0
func increment() { count += 1 } func decrement() { count -= 1 } } -
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 } } -
՝ • ੍͕ڧ͍ɺ࣮ίετॏͦ͏ • ෭࡞༻ͷஔ͖ॴɻMiddleware ? ActionCreator ? AsyncActionCreator ?
• Middleware Α͘ߟ࣮͑ͯ͠ͳ͍ͱΧΦεʹ ͳΓͦ͏ ( redux-saga ͷΑ͏ͳͷ͕ཉ͍͠ ) -
ࢀߟ 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