RxSwift分页

时间:2017-04-15 07:48:41

标签: swift rx-swift moya

我无法让这个解决方案起作用: https://github.com/liuznsn/RxMoyaPaginationNetworking

也许有人可以告诉我错误在哪里。加载变量永远不会变为false。我想这个问题在请求中是可观察的,但我找不到原因。

class PaginationNetworkModel<T1: Mappable>: NSObject {

let refreshTrigger = PublishSubject<Void>()
let loadNextPageTrigger = PublishSubject<Void>()
let loading = Variable<Bool>(false)
let elements = Variable<[T1]>([])
var offset:Int = 0
let error = PublishSubject<Swift.Error>()

private let disposeBag = DisposeBag()

override init() {
    super.init()

    let refreshRequest = loading.asObservable()
        .sample(refreshTrigger)
        .flatMap { [unowned self] loading -> Observable<[T1]> in
            if loading {
                return Observable.empty()
            } else {
                return self.loadData(offset: self.offset)
            }
    }

    let nextPageRequest = loading.asObservable()
        .sample(loadNextPageTrigger)
        .flatMap { [unowned self] loading -> Observable<[T1]> in
            if loading {
                return Observable.empty()
            } else {
                self.offset += 1
                return self.loadData(offset: self.offset)
            }
    }

    let request = Observable
        .of(refreshRequest, nextPageRequest)
        .merge()
        .shareReplay(1)

    let response = request.flatMap { events -> Observable<[T1]> in
        request
            .do(onError: { error in
                self.error.onNext(error)
            }).catchError({ error -> Observable<[T1]> in
                Observable.empty()
            })
    }.shareReplay(1)

    Observable
        .combineLatest(request, response, elements.asObservable()) { [unowned self] request, response, elements in
            return self.offset == 0 ? response : elements + response
        }
        .sample(response)
        .bind(to: elements)
        .addDisposableTo(rx_disposeBag)

    Observable
        .of(request.map { _ in true },
            response.map { $0.count == 0 },
            error.map { _ in false }
        )
        .merge()
        .bind(to: loading)
        .addDisposableTo(rx_disposeBag)
}

func loadData(offset: Int) -> Observable<[T1]> {
    return Observable.empty()
}

2 个答案:

答案 0 :(得分:4)

感谢Daniel的帮助,这里完成了解决方案

致电示例:

\b

型号代码:

goodsModel = GoodsNetworkModel()
    goodsModel.elements.asObservable().bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: StoreCell.self)) { (ip, item: Goods, cell: StoreCell) in
        cell.configure(goods: item)
    }.addDisposableTo(rx_disposeBag)

    tableView.rx.itemSelected.bind() { [unowned self] ip in
        self.didSelectRow(ip: ip.row)
    }.addDisposableTo(rx_disposeBag)

    rx.sentMessage(#selector(UIViewController.viewDidAppear(_:)))
        .map { _ in () }
        .bind(to: goodsModel.refreshTrigger)
        .addDisposableTo(rx_disposeBag)

    tableView.rx_reachedBottom
        .map{ _ in ()}
        .bind(to: goodsModel.loadNextPageTrigger)
        .addDisposableTo(rx_disposeBag)

答案 1 :(得分:3)

问题在于:

    let refreshRequest: Observable<[T1]> = loading.asObservable()
        .sample(refreshTrigger)
        .flatMap { [unowned self] loading -> Observable<[T1]> in
            if loading {
                return Observable.empty()
            } else {
                return self.loadData(offset: self.offset)
            }
    }

在 loadData返回之后,refreshRequest不会发出值。构建代码的方式,在refreshTrigger上发出信号将启动网络请求,然后在网络请求完成后将加载设置为true。

最好让refreshRequestnextPageRequest返回要加载的页面的Observable,然后合并它们并通过合并结果上的网络调用调用flatMap