Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

ReactorKitを利用した緩やかなアーキテクチャの移行

 ReactorKitを利用した緩やかなアーキテクチャの移行

2018.03.22 iOS Flux / Redux 勉強会 の登壇資料です

https://wantedly.connpass.com/event/79914/

Jiro Nagashima

March 22, 2018
Tweet

More Decks by Jiro Nagashima

Other Decks in Technology

Transcript

  1. ©2018 Wantedly, Inc. Wantedly Visit ΞϓϦͱ։ൃମ੍ Wantedly Visit ΞϓϦͷվम ReactorKit

    ͷಋೖ ReactorKit Λར༻࣮ͨ͠૷ ReactorKitಋೖޙͷؾ͖ͮ ໨࣍
  2. ©2018 Wantedly, Inc. Wantedly Visit ΞϓϦͱ։ൃମ੍ ‣ ࣮૷มߋͷݕ౼͕ඞཁ w J04ΞϓϦ͸ެ։͞Εͯ೥͕ܦա

    w Objective-C͔ΒSwiftͷ࣮૷΁Ҡߦ w ϦϦʔε౰ॳ͔ΒͷAPIΫϥΠΞϯτ࣮૷͕ଘࡏ ‣ ݶΒΕͨϦιʔεͰ։ൃΛਐΊΔඞཁੑ w ৽نࣄۀʹϦιʔε͕ूத w େن໛ͳ࣮૷มߋΛߦͳ͏ϦιʔεΛ֬อͰ͖ͳ͍ ‣ ΤϯδχΞͷۀ຿͸୯७ͳ։ൃ͚ͩͰͳ͍ • த௕ظͷࢪࡦͷܾఆɾ࣮ࢪΛΤϯδχΞࣗ਎͕ߦͳ͏
  3. ©2018 Wantedly, Inc. ΞʔΩςΫνϟҠߦͷഎܠ ‣ ࣮૷มߋͷݕ౼͕ඞཁ ‣ ݶΒΕͨϦιʔεͰ։ൃΛਐΊΔඞཁੑ ‣ ΤϯδχΞͷۀ຿͸୯७ͳ։ൃ͚ͩͰͳ͍

    େن໛ͳ࣮૷มߋΛߦͳΘͣɺվળࢪࡦͷ࣮ࢪͱซͤͯ ෦෼త͔ͭஈ֊తͳΞʔΩςΫνϟͷҠߦ͕ՄೳͳखஈΛٻΊ͍ͯͨ
  4. ©2018 Wantedly, Inc. վमՕॴืूը໘ ‣ ଟ༷ͳίϯςϯπ w ձࣾ৘ใɺϝϯόʔͷϓϩϑΟʔϧɺԠԉίϝϯτ ‣ ෳ਺ͷAPIϦΫΤετ

    w ืूͷৄࡉɺλάͷҰཡɺؔ࿈ืूͷҰཡ ‣ Ϣʔβʔૢ࡞ʹΑΔঢ়ଶมߋ w ϒοΫϚʔΫɺԠืɺԠԉ w දࣔϝϯόʔͷ੾Γସ͑
  5. ©2018 Wantedly, Inc. ‣ Fluxͷ࣮ݱ w ୯Ұํ޲ͷσʔλϑϩʔ w ঢ়ଶ؅ཧͷෳࡶ͞ͷղফ ‣

    Reactive ProgrammingΛԠ༻ w ετϦʔϜʹΑΔ6*ૢ࡞΍ঢ়ଶͷ఻ൖ w RxSwiftΛར༻ w ඇಉظॲཧͷෳࡶ͞ͷղফ ReactorKit
  6. ©2018 Wantedly, Inc. ReactorKit ಋೖͷܾΊख ‣ Flux ୯Ұํ޲ͷσʔλϑϩʔ ʹର͢Δظ଴ w

    Wantedly Visit ΞϓϦͷಛ௃ͱͯ͠ɺঢ়ଶ؅ཧΛඞཁͱ͢Δ༷ʑͳը໘Λ༗͢Δ w ଟ༷ͳίϯςϯπͷදࣔɺߴ౓ͳݕࡧϑΟϧλʔɺνϟοτɺϓϩϑΟʔϧฤू ‣ RxSwift ͱซ༻Մೳ w RxSwiftΛར༻͍ͯ͠ΔͷͰɺซ༻͢Δख๏Λཱ֬͢Δඞཁ͕ͳ͍ ‣ ෦෼త͔ͭஈ֊తʹಋೖ͕Մೳ • “Start Small”ͱ͍͏σβΠϯΰʔϧΛܝ͍͛ͯΔ
  7. ©2018 Wantedly, Inc. enum Action { case load case additionalLoad

    case toggleBookmarking case selectStaffing(Int) } Action ͷఆٛྫ ProjectViewReactor
  8. ©2018 Wantedly, Inc. struct State { var project: Project var

    summaryTags: [ProjectSummaryTag] = [] var relatedProjectSections: [ProjectRelatedProjectSection] = [] var companyProjects: [Project] = [] var isProjectLoaded: Bool = false var isBookmarking: Bool var isApplying: Bool var isSupporting: Bool var selectedStaffing: Staffing? } State ͷఆٛྫ ProjectViewReactor
  9. ©2018 Wantedly, Inc. func mutate(action: Action) -> Observable<Mutation> { switch

    action { case .load: return Observable .merge( Observable .concat([ projectService.project(id: currentState.project.id).asObservable().map { .setProject($0) }, Observable.just(.setProjectLoaded(true)), ]), projectService.summaryTags(projectID: currentState.project.id).asObservable().map { .setSummaryTags($0) } ) … } mutate ϝιουͷ࣮૷ྫ ProjectViewReactor
  10. ©2018 Wantedly, Inc. func reduce(state: State, mutation: Mutation) -> State

    { switch mutation { case let .setProject(project): var state = state state.project = project state.isBookmarking = project.isBookmarking state.isApplying = project.isApplied state.isSupporting = project.isSupporting state.selectedStaffing = state.selectedStaffing ?? project.staffings?.first return state … } reduce ϝιουͷ࣮૷ྫ ProjectViewReactor
  11. ©2018 Wantedly, Inc. ProjectViewController func bind(reactor: Reactor) { rx.methodInvoked(#selector(viewDidLoad)) .map

    { _ in Reactor.Action.load } .bind(to: reactor.action) .disposed(by: disposeBag) … state .map { $0.isBookmarking } .distinctUntilChanged() .bind(to: bookmarkButton.rx.isSelected) .disposed(by: disposeBag) … } bind ϝιουͷ࣮૷ྫ
  12. ©2018 Wantedly, Inc. ؾ͖ͮ ‣ ςετख๏ཱ͕֬͞Ε͍ͯΔ w ԿΛͲ͏ςετ͢΂͖͔໌ݴ͞Ε͍ͯͯɺखஈ΋༻ҙ͞Ε͍ͯΔ w ςετख๏ཱ͕֬͞Εͣςετ͕ॻ͔Εͳ͍ͱ͍͏ࣄଶ͕ى͜ΓͮΒ͍

    ‣ ඇΞϓϦΤϯδχΞ͕ΞʔΩςΫνϟΛཧղ͠΍͍͢ w ࣾ಺Ͱ͸webͷϑϩϯτ࣮૷ͰRedux Λར༻͍ͯ͠ΔͷͰલఏ஌͕ࣝ͋Δ w ReactorKitͷγϯϓϧ͞΋ड͚ೖΒΕ΍͍͢ ‣ αϯϓϧ͕๛෋ w ෳ਺ͷن໛ͷΞϓϦͷαϯϓϧ͕ެ։͞Ε͍ͯΔ w ඞཁͳ࣮૷ྫ͕ݟ͔ͭΔʢάϩʔόϧͳঢ়ଶͷѻ͍ɺςετίʔυͳͲʣ