RxSwift chapter
- first
- Sequential response logic
- Sequence types
- The operator
- Scheduler – Scheduler (1)
- Scheduler – Scheduler (2)
- Common sequence
- Subject is both offensive and defensive
- MVVM bidirectional binding
case
- Input box search – request network – tableView linkage
VC code
/ / search box to bind to the viewModel search sequence of the self. The searchBar. Rx. Text. OrEmpty. Bind (to: the self. The viewModel. SearchTextOB) disposed (by: Disposebag) / / search data binding to the tableView self. The viewModel. SearchData. Drive (self. TableView. Rx. The items) {(TB, indexPath, model) - > PLTableViewCellin
let cell = tb.dequeueReusableCell(withIdentifier: "PLTableViewCell") as! PLTableViewCell
cell.setData(model)
cell.selectionStyle = .none
returnCell}. Disposed (by: self. Disposebag) / / search data self. The viewModel. SearchData. Map {(array) - > Stringin
return "Found \(array.count) \
}
.drive(self.navigationItem.rx.title)
.disposed(by: disposebag)
Copy the code
- Set up binding dependencies between View and ViewModel in ViewController in RxSwift world.
- With the response from our search bar, we send data to our ViewModel, let it process the business layer internally, and finally return the data.
The model code
The class PLComprehensiveViewModel: NSObject {/ / sequence search keywordslet searchTextOB = BehaviorSubject(value: ""Lazy var searchData: Driver<[PLReposityModel]> = {// Make use of the input box sequencereturn self.searchTextOB.asObservable()
.throttle(RxTimeInterval.milliseconds(300), scheduler: MainScheduler.instance)
.distinctUntilChanged()
.flatMapLatest(PLComprehensiveViewModel.responseData)
.asDriver(onErrorJustReturn: [])
}()
...
}
Copy the code
-
The search searchTextOB associates changes to the input field.
-
The searchData sequence searchData ensures that events are sent at 0.3 seconds intervals by throttle
-
The distinctUntilChanged function guarantees bandwidth until the search keyword changes
-
FlatMapLates because of the sequence of the sequence, we sank the request
-
AsDriver is packaged as a Drive sequence to ensure state sharing, main thread scheduling, and no error return
Static func responseData(_ githubID: String) -> Observable<[PLReposityModel]> {guard! githubID.isEmpty,let url = URL(string: "https://api.github.com/users/\(githubID)/repos") else {
return Observable.just([])
}
return URLSession.shared.rx.json(url: url)
.retry()
.observeOn(ConcurrentDispatchQueueScheduler(qos: .background))
.map(PLComprehensiveViewModel.dataParse)
}
Copy the code
-
Through the above response, here enclosed network URLSession. Shared. Rx. Json, return json
-
To ensure that the request network is in child threads, observeOn is called
-
The requested data is not yet what we want, and the result is serialized by map mapping
Static func dataParse(_ json:Any) -> [PLReposityModel]{///let items = json as? [[String:Any]] else { return[]} /// Model guardlet result = [PLReposityModel].deserialize(from: items) as? [PLReposityModel] else{
return[]}return result
}
Copy the code
-
Data parsing via HandJSON
-
The result sequence is returned as an array of models
conclusion
- VC lightweight
- The combination of MVVM and RxSwift is comfortable