Tengo una aplicación con arquitectura en capas en la que obtengo una variedad de objetos en ViewModel y los vinculo con tableview en ViewController. El siguiente es mi código: ViewModel:
func getManufacturerList() -> Single<[ManufacturerItem]> { return self.fetchManufacturerInteractor.getManufacturerList() .map { $0.map { ManufacturerItem( manufacturerName: $0.manufacturerName, manufacturerID: $0.manufacturerID) } } } La función anterior recibe una matriz de objetos de alguna otra capa que nuevamente la obtiene de NetworkLayer.
Controlador de vista:
private func setUpViewBinding() { manufacturerViewModel.getManufacturerList() .asObservable() .bind(to: tableView.rx.items(cellIdentifier: LanguageSelectionTableViewCell.Identifier, cellType: LanguageSelectionTableViewCell.self)) { row, manufacturer, cell in cell.textLabel?.text = manufacturer.manufacturerName cell.textLabel?.font = AppFonts.appBoldFont(size: 16).value cell.accessibilityIdentifier = "rowLanguage\(row+1)" cell.textLabel?.accessibilityIdentifier = tblCellLabelAccessibilityIdentifier }.disposed(by: self.disposeBag) }Ahora, ¿dónde debo agregar el código para mostrar/ocultar el indicador de actividad?
ViewModel debe manejar mostrar u ocultar IndicatorView (si hay una sola vista de carga) porque su vista debe ser tonta, use BehaviorRelay en lugar de variable (la variable está obsoleta)
en vistaModelo
// create a subject and set the starter state, every time your viewModel // needs to show or hide a loading, just send an event let showLoading = BehaviorRelay<Bool>(value: true) // your async function func getManufacturerList() -> Observable { // notify subscriber to show the indicator view showLoading.accept(true) // do some works // notify subscribers to hide the indicator view showLoading.accept(false) }y en tu controlador de vista
// bind your indicator view to that subject and wait for events showLoading.asObservable().observeOn(MainScheduler.instance).bind(to: indicatorView.rx.isHidden).disposed(by: disposeBag)