我编写了如下的小演示代码。我制作了两个不同类型的 PublishSubject。当我更改任何页面触发器时,我只需要在一个更改时获取页面触发器,哪个observable_page。
class ViewController: UIViewController {
func loadData(page: Int, keyword: String) -> Observable<[Int]> {
let _result = Observable.of([1,2,3,4])
return _result
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let observable_keyword = PublishSubject<String>()
let observable_page = PublishSubject<Int>()
let trigger_tap = PublishSubject<Void>()
let tapObservable = trigger_tap.debug("trigger_tap", trimOutput: true)
let stringObservable = observable_keyword.debug("stringObservable", trimOutput: true)
let pageObservable = observable_page.debug("pageObservable", trimOutput: true)
let request_call_trigger = Observable.combineLatest(tapObservable, pageObservable)
.debug("request_call_trigger", trimOutput: true)
let page = request_call_trigger
.withLatestFrom(stringObservable) { ($0, $1) }
.flatMap { ((_, _, page), keyword) in
Observable.combineLatest(Observable.just(page), self.loadData(page: page, keyword: keyword)) { (pageNumber: $0, movies: $1) }
.materialize()
.filter { $0.isCompleted == false }
}
.share()
observable_keyword.onNext("breaking bad")
observable_page.onNext(1)
trigger_tap.onNext(())
observable_keyword.onNext("orange is new black")
observable_keyword.onNext("orange")
}
let bag = DisposeBag()
}
我读了一些选项,觉得过滤器或忽略可能在这里有用,但是因为我需要它们的价值,所以很困惑,如何正确应用它。