想在 iOS 中使用 RxSwift 制作搜索过滤器

want to make search filter using RxSwift in iOS

我有 table 视图,其中包含我尝试制作 searchBar 并按书名搜索的图书列表

this is HomeViewModel

private var homeModelSubject      = PublishSubject<[Book]>()
private var filterModelSubject    = PublishSubject<[Book]>()
private var isTableHidden         = BehaviorRelay<Bool>(value: false)
var searchValueBehavior           = BehaviorRelay<String>(value: "")

var homeModelObservable: Observable<[Book]> {
    return homeModelSubject
}

var filterModelObservable: Observable<[Book]> {
    return filterModelSubject
}

var isTableHiddenObservable:Observable<Bool> {
    return isTableHidden.asObservable()
}

var serchValueObservable: Observable<String> {
    return searchValueBehavior.asObservable()
}

init() {
    serchValueObservable.subscribe(onNext: { value in
        self.homeModelObservable.map({ [=11=].filter ({
            if value.isEmpty {return true}
            return ([=11=].name.lowercased().contains(value.lowercased()))
        })
        }).bind(to: self.filterModelSubject).disposed(by: self.disposeBag)
    }).disposed(by: disposeBag)
}

this function to make bind to text value

func bindToSearchValue() {
    searchController.searchBar.rx.text.orEmpty.throttle(.milliseconds(500), scheduler: MainScheduler.instance).distinctUntilChanged()
        .bind(to: homeViewModel.searchValueBehavior).disposed(by: disposeBage)
}

this function to subscribe to response

func subscribeToResponse() {
    homeViewModel.filterModelObservable.bind(to: self.tableView.rx.items(cellIdentifier: HomeTableViewCell.reuseIdentifier, cellType: HomeTableViewCell.self)) { row,books,cell in
        cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: UIScreen.main.bounds.width)
        cell.titleLabel.text           = books.name
        cell.secondaryTitleLabel.text  = books.type
        cell.selectionStyle            = .none
        
        if books.available == true {
            cell.avalibaleOrNotLabel.text = "Avalibale"
            cell.avalibaleOrNotStatus.backgroundColor = .green
        } else {
            cell.avalibaleOrNotLabel.text = "Not Avalibale"
            cell.avalibaleOrNotStatus.backgroundColor = .gray
        }
        
    }.disposed(by: disposeBage)
}

问题----> make search时没有过滤单元格

The essence of functional reactive programming is to specify the dynamic behavior of a value completely at the time of declaration. -- Heinrich Apfelmus

您的视图模型应如下所示:

class HomeViewModel {
    let filterModelObservable: Observable<[Book]>
    var searchValueObserver: AnyObserver<String?> { searchValueBehavior.asObserver() }
    var homeModelObservable: Observable<[Book]> { homeModelSubject }
    var isTableHiddenObservable:Observable<Bool> { isTableHidden.asObservable() }
    private let homeModelSubject = PublishSubject<[Book]>()
    private let isTableHidden = BehaviorRelay<Bool>(value: false)
    private let searchValueBehavior = BehaviorSubject<String?>(value: "")

    init() {
        filterModelObservable = Observable.combineLatest(
            searchValueBehavior
                .map { [=10=] ?? "" }
                .startWith("")
                .throttle(.milliseconds(500), scheduler: MainScheduler.instance),
            homeModelSubject
        )
        .map { searchValue, books in
            searchValue.isEmpty ? books : books.filter { [=10=].name.lowercased().contains(searchValue.lowercased()) }
        }
    }
}

请注意 filterModelObservable 将发出的值是如何在创建它的代码中完全指定的。

你不应该需要那么多主题:

Subjects provide a convenient way to poke around Rx, however they are not recommended for day to day use. -- Intro to Rx

另请注意,没有disposeBag。你不应该在你的视图模型中需要一个处理包,否则你可能做错了什么。

您应该尽可能减少视图控制器中包含的逻辑。它应该只包含赋值和绑定。所以你的 bindToSearchBar 应该是这样的:

func bindToSearchValue() {
    searchController.searchBar.rx.text
        .bind(to: homeViewModel.searchValueObserver)
        .disposed(by: disposeBage)
}

(您可能还想更正 disposeBage 中的拼写错误。)