今更学ぶReSwift

 今更学ぶReSwift

69a41c1eba0ed2c23806dd9172763c74?s=128

Keisuke Kimura

October 25, 2020
Tweet

Transcript

  1. ࠓߋֶͿReSwift @cor0suke_k Functional Architecture৘ใڞ༗ձ

  2. ࠓߋֶͿReSwift Agenda • ͳͥࠓߋReSwift? • ReSwiftͱ͸ • ReSwiftͷ؆୯ͳαϯϓϧ • ඇಉظॲཧͲ͏ͯ͠Δ͔

    • ReSwiftͷ͍ܰ͸·ΓͲ͜Ζ • ࠷ޙʹ
  3. ͳͥࠓߋReSwift?

  4. ͳͥࠓߋReSwift? • UIKit͔ΒSwiftUI΁ͷҠߦظʹೖΖ͏ͱ͢Δத UIKitͰ΋࢖͑ΔReduxܥͷϥΠϒϥϦΛ஌͓ͬͯ͘ͷ΋ѱ͘͸ͳ͍͔ͳͱࢥͬͯʂ • ReSwift͸ͦͦ͜͜ރΕͯΔϥΠϒϥϦ -> ࢖͍΍͍͢ • Swiftʹ͸ͪΌΜͱ௥ै͍ͯ͠Δ͙Β͍ͷ׆ൃ౓͸͋Δ

    • ͕͔͠͠ൃల΋Α͋͘Δ ຊՈReduxͷਐԽʹ௥ैͨ͠Γ͢Δ -> Redux-thunk͕ੜ·Εͨ͜ͱͰReSwift-thunk΋ੜ ·Εͨʢޙड़ʣ • TCA࢖͏ਓͱ͔ͷࢀߟʹͳΕ͹ͳ͙Β͍ͳؾ࣋ͪ
  5. ReSwiftͱ͸

  6. Redux + Swift

  7. ReSwiftͱ͸ • Redux-likeͳ୯ํ޲σʔλϑϩʔΞʔΩςΫνϟΛߏங͢ΔϥΠϒϥϦ • ओͳ໨త͸ΞϓϦͷίϯϙʔωϯτͷ͋Δࡾͭͷؔ৺ࣄΛ෼཭͢Δͷ͕໨త • ঢ়ଶͷ؅ཧ • Ϗϡʔͷߋ৽ •

    ঢ়ଶͷมߋ
  8. ग़య: http://reswift.github.io/ReSwift/master/about-reswift.html

  9. ग़య: http://reswift.github.io/ReSwift/master/getting-started-guide.html

  10. ReSwiftͷ̏ཁૉ • Store • Action • Reducer

  11. ReSwiftͷ̏ཁૉ • Store ΞϓϦશମͷঢ়ଶΛ୯Ұͷσʔλߏ଄(State)Ͱอଘ͢Δɻ มߋ͕͋Ε͹௨஌͢Δ • Action • Reducer

  12. ReSwiftͷ̏ཁૉ • Store ΞϓϦશମͷঢ়ଶΛ୯Ұͷσʔλߏ଄(State)Ͱอଘ͢Δɻ มߋ͕͋Ε͹௨஌͢Δ • Action ঢ়ଶͷมԽΛ௨஌͢ΔͨΊͷํ๏ɻActionʹ͸࣮ࡍͷมߋΛ͢Δίʔυ͸ؚ ·ͣछྨΛStoreΛ௨ͯ͡ReducerʹૹΔɻ •

    Reducer
  13. ReSwiftͷ̏ཁૉ • Store ΞϓϦશମͷঢ়ଶΛ୯Ұͷσʔλߏ଄(State)Ͱอଘ͢Δɻ มߋ͕͋Ε͹௨஌͢Δ • Action ঢ়ଶͷมԽΛ௨஌͢ΔͨΊͷํ๏ɻActionʹ͸࣮ࡍͷมߋΛ͢Δίʔυ͸ؚ·ͣ छྨΛStoreΛ௨ͯ͡ReducerʹૹΔɻ •

    Reducer : (state, action) -> State ݱࡏͷStateͱૹΒΕ͖ͯͨActionΛ΋ͱʹมߋΛߦ͍৽ͨͳStateΛStoreʹฦ͢
  14. import ReSwift struct AppState: StateType { var count = 0

    } // This action does not have state and is a mere marker of "X happened": enum CounterAction: Action { case increase case decrease } func appReducer(action: Action, state: AppState?) -> AppState { var state = state ?? AppState() if let counterAction = action as? CounterAction { switch counterAction { case .increase: state.counter += 1 case .decrease: state.counter -= 1 } } return state } let store = Store( reducer: appReducer, state: AppState(), middleware: []) store.dispatch(CounterAction.increase)
  15. ඇಉظॲཧͲ͏ͯ͠Δ͔

  16. ඇಉظॲཧͲ͏ͯ͠Δ͔ • ௨ৗͷAction+ReducerͰ͸ඇಉظॲཧΛදΘ͢ͷ͸ݫ͍͠ ReSwiftͰ͸ReSwift-thunkͱ͍͏΋ͷΛ࢖͏ • ੲ͸ActionCreatorͱ͍͏΋ͷ͕͕͋ͬͨDeprecatedʹͳͬͨ • ඇಉظΛߦͬͯActionΛվΊͯdispatch͢ΔྲྀΕ

  17. import ReSwift import ReSwiftThunk // First, you create the middleware,

    which needs to know the type of your `State`. let thunkMiddleware: Middleware<AppState> = createThunkMiddleware() // Note that it can perfectly live with other middleware in the chain. let store = Store<AppState>(reducer: reducer, state: nil, middleware: [thunkMiddleware])
  18. // A thunk represents an action that can perform side

    effects, access the current state of the store, and dispatch new actions, as if it were a ReSwift middleware. let thunk = Thunk<AppState> { dispatch, getState in if getState!.loading { return } dispatch(RequestStart()) api.getSomething() { something in if something != nil { dispatch(RequestSuccess(something)) } else { dispatch(RequestError()) } } } // As the thunk type conforms to the `Action` protocol, you can dispatch it as usual, without having to implement an overload of the `dispatch` function inside the ReSwift library. store.dispatch(thunk)
  19. // A thunk can also be a function if you

    want to pass on parameters func thunkWithParams(_ identifier: Int) -> Thunk<AppState> { return Thunk<MyState> { dispatch, getState in guard let state = getState() else { return } if state.loading { return } api.getSomethingWithId(identifier) { something in if something != nil { dispatch(RequestSuccess(something)) } else { dispatch(RequestError()) } } } } // You can do the same with the Thunk that requires parameters, like so store.dispatch(thunkWithParams(10))
  20. ReSwiftͷ͍ܰ͸·ΓͲ͜Ζ

  21. ReSwiftͷ͍ܰ͸·ΓͲ͜Ζ • ެࣜΨΠυ(Readme)ʹὃ͞Εͳ͍ Action͕structͰ࡞ΒΕ͍ͯΔ͚Ͳɺ࣮ࡍͷݱ৔Ͱ͸enumͰ࡞ΒΕ͍ͯΔ ࣮͸SampleͷAction΋৽͍͠΄͏͸enumͰॻ͍ͯ͋Δ ެࣜΨΠυͷԞਂ͘ʹ͸enumਪ঑ͬͯॻ͍ͯ͋Δ • ඇಉظॲཧʹؔͯ͠Sample͸ActionCreatorΛ࢖ͬͯΔ͚ͲDeprecated ReSwift-ThunkΛ࢖༻͢Δ͜ͱͱͳ͍ͬͯΔʢॻ͍ͯͳ͍͚Ͳʣ

  22. ࠷ޙʹ

  23. ࠷ޙʹ • SwiftUI൛͸Ӷҙ։ൃத @ObservedObjectͱͯ͠StateΛѻ͑ΔΑ͏ʹ͢Δαϯϓϧ͸ͪ͜Β https://github.com/ReSwift/ReSwift/issues/424#issuecomment- 532050246 • UIKitΛओʹ࢖ͬͯঃʑʹSwiftUIʹҠߦ͢ΔϓϩδΣΫτͩͱ࢖͑Δʢ͔΋ʣ • ࠷ॳ͔ΒSwiftUIΦϯϦʔΛ໨ࢦ͢ϓϩδΣΫτͳΒଞͷΛ࢖͓͏ʂ

    TCAͱ͔ʂBow-archͱ͔ʂ