RxSwift .debounce、.throttle 用于分页

问题描述 投票:0回答:1

我正在尝试实现分页。每当表视图滚动时,我希望反应器检查它是否到达底部。但是每当

reactor.action
tableView
发生时绑定
didScroll
似乎没有必要。

所以我认为最好使用运算符来限制绑定

reactor.action
。但在这种情况下,我不确定
.debounce
.throttle
之间哪个运算符更合适。

self.tableView.rx.didScroll
    .skip(1)
    .withLatestFrom(self.tableView.rx.contentOffset)
    .map { [weak self] in
        print("didScroll")
        
        return Reactor.Action.pagination(
            contentHeight: self?.tableView.contentSize.height ?? 0,
            contentOffsetY: $0.y,
            scrollViewHeight: UIScreen.main.bounds.height
        )
    }
    .bind(to: reactor.action)
    .disposed(by: disposeBag)

我在我的代码中尝试过

.debounce
.throttle
。但还是不知道哪个更合适。

swift rx-swift
1个回答
0
投票

就我个人而言,我总是使用

RxSwiftExt
中提供的 reachedBottom(offset:) 方法。

也就是说,回答问题......

  • debounce
    运算符适用于输入不稳定并且您想要等到它稳定下来才能读取输入的情况。

  • throttle
    运算符适用于输入不断发出并且您希望将流速度减慢至可管理的数字时。

  • 当输出始终相同(例如,

    throttle
    )并且您希望立即做出反应时,您还可以使用
    ()
    。在这种情况下,您需要确保将
    latest
    设置为 false。

我假设您在这里想要的是等到用户停止滚动,然后检查 contentOffset 的

y
(我希望您已经根据上面的描述知道要使用哪个。)

在这种情况下使用

debounce

© www.soinside.com 2019 - 2024. All rights reserved.