Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

ࠓߋֶͿReSwift Agenda • ͳͥࠓߋReSwift? • ReSwiftͱ͸ • ReSwiftͷ؆୯ͳαϯϓϧ • ඇಉظॲཧͲ͏ͯ͠Δ͔ • ReSwiftͷ͍ܰ͸·ΓͲ͜Ζ • ࠷ޙʹ

Slide 3

Slide 3 text

ͳͥࠓߋReSwift?

Slide 4

Slide 4 text

ͳͥࠓߋReSwift? • UIKit͔ΒSwiftUI΁ͷҠߦظʹೖΖ͏ͱ͢Δத UIKitͰ΋࢖͑ΔReduxܥͷϥΠϒϥϦΛ஌͓ͬͯ͘ͷ΋ѱ͘͸ͳ͍͔ͳͱࢥͬͯʂ • ReSwift͸ͦͦ͜͜ރΕͯΔϥΠϒϥϦ -> ࢖͍΍͍͢ • Swiftʹ͸ͪΌΜͱ௥ै͍ͯ͠Δ͙Β͍ͷ׆ൃ౓͸͋Δ • ͕͔͠͠ൃల΋Α͋͘Δ ຊՈReduxͷਐԽʹ௥ैͨ͠Γ͢Δ -> Redux-thunk͕ੜ·Εͨ͜ͱͰReSwift-thunk΋ੜ ·Εͨʢޙड़ʣ • TCA࢖͏ਓͱ͔ͷࢀߟʹͳΕ͹ͳ͙Β͍ͳؾ࣋ͪ

Slide 5

Slide 5 text

ReSwiftͱ͸

Slide 6

Slide 6 text

Redux + Swift

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

ग़య: http://reswift.github.io/ReSwift/master/about-reswift.html

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

ReSwiftͷ̏ཁૉ • Store • Action • Reducer

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

ReSwiftͷ̏ཁૉ • Store ΞϓϦશମͷঢ়ଶΛ୯Ұͷσʔλߏ଄(State)Ͱอଘ͢Δɻ มߋ͕͋Ε͹௨஌͢Δ • Action ঢ়ଶͷมԽΛ௨஌͢ΔͨΊͷํ๏ɻActionʹ͸࣮ࡍͷมߋΛ͢Δίʔυ͸ؚ·ͣ छྨΛStoreΛ௨ͯ͡ReducerʹૹΔɻ • Reducer : (state, action) -> State ݱࡏͷStateͱૹΒΕ͖ͯͨActionΛ΋ͱʹมߋΛߦ͍৽ͨͳStateΛStoreʹฦ͢

Slide 14

Slide 14 text

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)

Slide 15

Slide 15 text

ඇಉظॲཧͲ͏ͯ͠Δ͔

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

import ReSwift import ReSwiftThunk // First, you create the middleware, which needs to know the type of your `State`. let thunkMiddleware: Middleware = createThunkMiddleware() // Note that it can perfectly live with other middleware in the chain. let store = Store(reducer: reducer, state: nil, middleware: [thunkMiddleware])

Slide 18

Slide 18 text

// 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 { 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)

Slide 19

Slide 19 text

// A thunk can also be a function if you want to pass on parameters func thunkWithParams(_ identifier: Int) -> Thunk { return Thunk { 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))

Slide 20

Slide 20 text

ReSwiftͷ͍ܰ͸·ΓͲ͜Ζ

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

࠷ޙʹ

Slide 23

Slide 23 text

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