gpt4 book ai didi

swift - RxSwift 分页

转载 作者:搜寻专家 更新时间:2023-10-31 08:16:15 26 4
gpt4 key购买 nike

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

也许有人可以告诉我哪里错了。 loading 变量永远不会变为 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()
}

最佳答案

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

调用示例:

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)

模型代码:

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 { loading -> Observable<Int> in
if loading {
return Observable.empty()
} else {
return Observable<Int>.create { observer in
observer.onNext(0)
observer.onCompleted()
return Disposables.create()
}
}
}

let nextPageRequest = loading.asObservable()
.sample(loadNextPageTrigger)
.flatMap { [unowned self] loading -> Observable<Int> in
if loading {
return Observable.empty()
} else {
return Observable<Int>.create { [unowned self] observer in
self.offset += 1
observer.onNext(self.offset)
observer.onCompleted()
return Disposables.create()
}
}
}

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

let response = request.flatMap { offset -> Observable<[T1]> in
self.loadData(offset: offset)
.do(onError: { [weak self] 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()
}

关于swift - RxSwift 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43423597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com