我已经在视图控制器中编写了所有代码,我只是无法理解必须观察搜索文本的变化然后调用数据库方法,这将通过搜索文本过滤项目.
一些代码,我已经有了.
我的VIEwController
import Foundationimport UIKitimport RxSwiftimport RxCocoaclass 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) }}
我的观点模型:
import Foundationimport RxSwiftimport RxCocoaclass 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]>为我的DataBase创建反应性包装器.这是我的代码
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全部内容,希望文章能够帮你解决ios – 用于搜索屏幕的MVVM和RxSwift所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)