Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- readLine
- binder
- RxSwift
- UIResponder
- moya
- 사내배포
- AVCaptureSession
- MaxHeap
- hitTest
- URLSession
- Combine
- delays deallocation
- weak self
- UserDefaults
- Responder chain
- input
- ios
- swift
- Asnyc
- 공백
- async
- vtable
- Custom Class
- ReactorKit
- reversed
- 전자출입
- BidirectionalCollection
- Python
- DISPATCH
- 입력
Archives
- Today
- Total
틈틈히 적어보는 개발 일기
[TIL #7] ReactorKit 본문
⚠️요약
아래 Flow는 VC의 Bind(reactor: )에서
- View의 Input(Action)을 Reactor와 연결
- View의 Output(State)을 Reactor와 연결
1. UI의 Input을 Reactor.Action.— 로 mapping 후 reactor.action에 bind를 걸어 input 수행
ㄴ 1-1(내부 Flow). 정의된 mutate함수 수행 결과(Observable<Mutation>)를 reduce 함수로 전달, reduce 함수가 state를 변화시킴
2. reactor.state를 UI에 bind를 걸어 output 수행
// MARK: - Code
// 1. Action rx.viewWillAppear .mapToVoid() .map { Reactor.Action.fetch } .bind(to: reactor.action) .disposed(by: disposeBag) // 2. State reactor.state.map(\.title) .bind(to: titleLabel.rx.text) .disposed(by: disposeBag) // ========================================================================= // 1.1 Reactor 내부 Flow // Reactor.swift enum Action { case fetch } enum Mutation { case 제목업데이트(String) } struct State { var title: String } func mutate(action: Action) -> Observable<Mutation> { switch Action { case .fetch: return .just(Mutation.setItem(새로운제목)) } } func reduce(state: State, mutation: Mutation) -> State { var newState = state switch mutation { case .제목업데이트(let 새로운제목): state.title = 새로운제목 } }
action이 인풋, state가 아웃풋이라고 보면 된다!
- bindAction view의 이벤트를 Reactor로 전달
- bindState Reactor의 state 변화를 view로 방출(구독)
mutate()와 reduce()
mutate는 액션을 수행하는 역할. 액션 수행 로직을 작성하며 return 타입이 Observable<Mutation> 그리고 reduce에게 파라미터로 전달하여 새로운 state를 반환하고 이 state를 구독하고 있는 view가 자신을 업데이트
💡 VC에서 bind(reactor: ) 구현해두고 Reactor.Action.— 과 바인드를 하게 되면 Reactor에 선언된 mutate, reduce가 순서대로 수행됨!!!!
- mutate(): Action을 받고 Observable<Mutation>을 생성한다.
- reduce(): 이전의 state와 mutate()로 부터 생성된Observable<Mutation> 으로 새로운 state를 생성한다.
Action, Mutation, State
⚠️ reactorkit 특성상 구독을 걸어 놓으면 errorResult 값이 바뀌지 않아도 다른 state 값이 바뀌면 그전 값도 같이 내려오게 된다. 그래서 항상 distinctUntilChanged() 를 걸어놓는다. → 그러나 똑같은 값이 두번 내려올 경우에는 구독을 받을 수 없다!!!!!
- Observable.concat
Observable의 앞에 여러개의 element를 추가
→ Reactor에서는 새로 만든 Observable에 concat을 하므로 concat에 넣는 element만 방출!! - Action
View의 Input에 대한 정의.
mutation 함수에서 해당 action들에 대해 어떠한 행동을 할지 정의하고, perform~~~ 형식의 메소드로 빼기도 한다 - Mutation
Action의 결과물.
reduce에서 action의 결과물을 mutation의 통해 state 값을 변경시키고, 이를 반환함.
'📝 TIL' 카테고리의 다른 글
[TIL #6] RxSwift - Binder, MainThread에서 동작함을 확인하다 (0) | 2022.06.09 |
---|---|
[TIL #5] MaxHeap, 최대힙 (0) | 2022.04.18 |
[TIL #3] strong self, weak self, delays deallocation (0) | 2022.04.03 |
[TIL #2] 네트워크 코드를 더욱 깔끔하게 (feat: URLSession, Moya) (0) | 2022.03.31 |
[TIL #1] Swift에는 없는 자료구조: queue, deque (0) | 2022.03.21 |
Comments