#4 RxSwift Search and Filter a DataSource for a TableView - RxSwift MVVM Coordinator iOS App
asObservable
화 private let disposeBag = DisposeBag()
typealias State = (airports: BehaviorRelay<Set<AirportModel>>, ())
private let state: State = (airports: BehaviorRelay(value: []), ())
state
를 통해 현 시점에서 가지고 있는 공항 데이터 표현func process() {
airportService
.fetchAirports()
.map({ Set($0) })
.map({ [state] in state.airports.accept($0) })
.subscribe()
.disposed(by: disposeBag)
}
BehaviorRelay
에 포함시키기 static func transform(input: SearchViewPresentable.Input, state: State, bag: DisposeBag) -> SearchViewPresentable.Output {
let searchTextObservable = input.searchText
.debounce(.milliseconds(500))
.distinctUntilChanged()
.skip(1)
.asObservable()
.share(replay: 1, scope: .whileConnected)
let airportsObservable = state.airports
.skip(1)
.asObservable()
Observable
.combineLatest(searchTextObservable, airportsObservable)
.map { query, airports in
return airports.filter { airport -> Bool in
!query.isEmpty && airport.city.lowercased().replacingOccurrences(of: " ", with: "")
.hasPrefix(query.lowercased())
}
}
.subscribe()
.disposed(by: bag)
return ()
}
Observable
이 가지고 있는 combineLatest
를 통해 검색 쿼리문 관찰자 + 공항 데이터 관찰자를 동시에 체크 가능