我正在组合一个viewDidAppear并使用Rx Swift过滤Drivers.他们工作得很好.但是当我介绍第三个驱动程序时,它会停止在最新的组合上调用flatMapLatest. 在我的View Controller中,我有以下驱动程序:
在我的View Controller中,我有以下驱动程序:
let filter: Driver<String> let viewDidAppear: Driver<Void> let refresh: Driver<Void>
在我的视图模型中:
// On viewDidAppear, I download a list of portfolios let viewDidAppearPortfolios = viewDidAppear .flatMapLatest({ (_) -> Driver<Result<[PortfolioModel]>> in return networkService.request(Router.portfolios)! .responseCollections() .trackActivity(fetching) .asDriver(onErrorJustReturn: .failure(NSError())) }) .flatMapLatest({ (result: Result<[PortfolioModel]>) -> Driver<[PortfolioModel]> in switch result { case .success(let value): return Driver.just(value) case .failure(_): return Driver.just([]) } }) // Then I combine with a filter from my search bar. self.portfolios = Driver.combineLatest(viewDidAppearPortfolios, filter) .flatMapLatest { (portfolios: [PortfolioModel], filter: String) -> Driver<[PortfolioModel]> in if filter.isEmpty { return Driver.just(portfolios) } return Driver.just(portfolios.filter({ (portfolio) -> Bool in portfolio.portfolio.localizedCaseInsensitiveContains(filter) })) }
以上作品!
网络请求一个投资组合列表,我能够在我输入客户端时过滤这些结果.
但是,我希望用户拉动刷新,并再次触发网络请求!所以,我结合我的刷新驱动程序.
还有这个:
Driver.combineLatest(viewDidAppearPortfolios, filter)
变成这样:
Driver.combineLatest(viewDidAppearPortfolios, filter, refresh)
问题!
结合刷新后,不再在viewDidAppear上调用flatMapLatest!只有我手动pullToRefresh.
Driver.combineLatest(viewDidAppearPortfolios, filter, refresh).flatMapLatest { _,_,_ in // No longer get's called on viewDidAppear after combining with refresh }
> viewDidAppearPortfolios仍然执行,因此网络请求
被叫了!
>只有当我手动拉动刷新时才能获得
我之前要求的投资组合清单……
知道为什么吗?
谢谢!
看起来您的刷新还没有发出单个事件,因此不计算combineLatest.我试过这段代码来测试:
let one = Driver.just(1) let two = Driver.just(2) let three: Driver<Int> = .just(3) let result = Driver.combineLatest(one, two, three) .flatMapLatest { return Driver.just($0 + $1 + $2) } result.drive(onNext: { print($0) })
这打印6但是如果你使用let 3:Driver< Int> = .empty()这不是打印任何东西.所以我想你需要一种方法来设置初始值来刷新流.