Moya: How could I cancel requests when another request is made

Created on 7 Oct 2018  路  3Comments  路  Source: Moya/Moya

Moya: 11.0.2

I use RxMoya for a search, I start a request as the user types, how could I cancel the previous requests?

question stale

Most helpful comment

You can use flatMapLatest: flatMapLatest only emits elements from the most recent inner Observable sequence.

self.searchController.searchBar.rx.text
            .orEmpty
            .debounce(0.5, scheduler: MainScheduler.instance)
            .distinctUntilChanged()
            .filter { !$0.isEmpty && $0.count > 2 }
            .flatMapLatest { (query) -> Observable<String> in
                return network.searchUser(matching: query)
            }
            .subscribe(onNext: { users in
                self.users  = users
                //update view
            }).disposed(by: self.disposeBag)

All 3 comments

You can use flatMapLatest: flatMapLatest only emits elements from the most recent inner Observable sequence.

self.searchController.searchBar.rx.text
            .orEmpty
            .debounce(0.5, scheduler: MainScheduler.instance)
            .distinctUntilChanged()
            .filter { !$0.isEmpty && $0.count > 2 }
            .flatMapLatest { (query) -> Observable<String> in
                return network.searchUser(matching: query)
            }
            .subscribe(onNext: { users in
                self.users  = users
                //update view
            }).disposed(by: self.disposeBag)

This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

This issue has been auto-closed because there hasn't been any activity for at least 21 days. However, we really appreciate your contribution, so thank you for that! 馃檹 Also, feel free to open a new issue if you still experience this problem 馃憤.

Was this page helpful?
0 / 5 - 0 ratings