gpt4 book ai didi

ios - 用于搜索屏幕的 MVVM 和 RxSwift

转载 作者:可可西里 更新时间:2023-11-01 06:02:38 25 4
gpt4 key购买 nike

出于教育 MVVM 和 RxSwift 的目的,我想构建简单的搜索屏幕,它将有一个表格 View 和一个搜索栏。当用户在搜索栏中输入内容时,我将在该表中显示他所拥有的内容。听起来很简单,但我找不到适合我的教程。

我已经在 View Controller 中编写了所有代码,我只是无法理解必须观察搜索文本的变化然后调用数据库方法,该方法将按搜索文本过滤项目。

一些代码,我已经有了。

我的 View Controller

import Foundation
import UIKit
import RxSwift
import RxCocoa

class PlaceSearchViewController: UIViewController {

//MARK: -

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var tableView: UITableView!

//MARK: - Dependencies

private var viewModel: PlaceSearchViewModel!
private let disposeBag = DisposeBag()

//MARK: - Lifecycle

override func viewDidLoad() {
super.viewDidLoad()

viewModel = PlaceSearchViewModel()
addBindsToViewModel(viewModel)
}

//MARK: - Rx

private func addBindsToViewModel(viewModel: PlaceSearchViewModel) {

searchBar.rx_text.bindTo(viewModel.searchTextObservable)

viewModel.placesObservable.bindTo(tableView.rx_itemsWithCellFactory) {
(tableView: UITableView, index, place: Place) in

let indexPath = NSIndexPath(forItem: index, inSection: 0)
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as PlaceCell

cell.configureWithObject(place)

return cell

}
.addDisposableTo(disposeBag)

tableView.rx_contentOffset
.subscribe { _ in
if self.searchBar.isFirstResponder() {
_ = self.searchBar.resignFirstResponder()
}
}
.addDisposableTo(disposeBag)
}
}

还有我的 View 模型:

import Foundation
import RxSwift
import RxCocoa

class PlaceSearchViewModel {

//MARK: - Dependecies
private let disposeBag = DisposeBag()

//MARK: - Model

private let placesObservable: Observable<[Place]>
var searchTextObservable = Variable<String>("")

//MARK: - Set up

init() {

placesObservable = searchTextObservable.asObservable()
//wait 0.3 s after the last value to fire a new value
.debounce(0.3, scheduler: MainScheduler.instance)
//only fire if the value is different than the last one
.distinctUntilChanged()
//convert Observable<String> to Observable<[Place]>
.flatMapLatest { searchString -> Observable<[Place]> in

// some code here which I can't write.

}
//make sure all subscribers use the same exact subscription
.shareReplay(1)
}

}

此外,我还有方法 [DataBase searchPlaces:searchText] 返回位置数组 - [Place]。我无法理解它在我的 ViewModel 的 flatMapLatest 中的位置和方式。

最佳答案

我通过创建 Observable<[Place]> 为我的数据库创建响应式包装器。

这是我的代码

placesObservable = searchTextObservable.asObservable()
//wait 0.3 s after the last value to fire a new value
.debounce(0.0, scheduler: MainScheduler.instance)
//only fire if the value is different than the last one
.distinctUntilChanged()
//convert Observable<String> to Observable<Weather>
.flatMapLatest { searchString -> Observable<[AnyObject]> in
return TPReactiveDatabase.sharedInstance.searchPlacesByTitle(searchString)
}
//make sure all subscribers use the same exact subscription
.shareReplay(1)

和包装方法searchPlacesByTitle

class TPReactiveDatabase: NSObject {

static let sharedInstance = TPReactiveDatabase()

// MARK: - Reactive Place database

func searchPlacesByTitle(title: String) -> Observable<[AnyObject]> {
return Observable.create { observer in

var places = [AnyObject]()

if (title.characters.count > 0) {
places = DBAccessKit.searchPlacesByTitle(title)
}

observer.on(.Next(places))
observer.on(.Completed)

return AnonymousDisposable {

}
}
}
}

关于ios - 用于搜索屏幕的 MVVM 和 RxSwift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39115360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com