0

以下代码有效:

    let provider = RxMoyaProvider<MyAPI>( stubClosure: MoyaProvider.delayedStub(3))

    provider
        .request(.studentSearch(query: ""))
        .retry(3)
        .observeOn(MainScheduler.instance)
        .asObservable()
        .mapJSON()
        .map { respJSON in
            guard let studentsJsonArray = JSON(respJSON)["students"].array else {
                throw APIError.wrongJSONParsing
            }
            return studentsJsonArray.map {
                guard let students = Student.fromJSON($0) else {
                    fatalError("Invalid Student Object")
                }
                return students
                } as [Student]
        }
        .subscribe(onNext: {
          print($0)
        }, onCompleted: {
          print($0) // This one is being called.
        })
        .disposed(by: rx.disposeBag)

在上面的代码中调用了 onCompleted 方法,但在下面的代码中没有。

我正在尝试使用 UITableView 中的刷新触发器来刷新内容。我想在开始时加载内容,所以我startWith(())在 ViewModel 中使用以下代码

let results: Driver<[Student]>
var refreshTrigger = PublishSubject<Void>()
results = refreshTrigger
        .startWith(())
        .do(onNext: {
            execute.value = true
        })
        .flatMapLatest {
            provider
                .request(.studentSearch(query: ""))
                .retry(3)
                .observeOn(MainScheduler.instance)
                .asObservable()
        }
        .mapJSON()
        .map { respJSON in
            guard let studentsJsonArray = JSON(respJSON)["students"].array else {
                throw APIError.wrongJSONParsing
            }
            return studentsJsonArray.map {
                guard let students = Student.fromJSON($0) else {
                    fatalError("Invalid Student Object")
                }
                return students
            }
        }
        .do(onNext: {
            items.value = $0
            execute.value = false
            noResults.value = items.value.isEmpty
        }, onCompleted: {
            print($0)
        })
        .asDriver(onErrorJustReturn: [])

在控制器中,我在 viewdidload 中调用以下内容。

viewModel
        .results
        .asObservable()
        .map { StudentGroup(header: "Follower", items: $0) }
        .subscribe(onNext: {
            print($0)
        }, onCompleted: {
            print($0) // This is not being called.
        })
        .disposed(by: rx.disposeBag)

这里没有调用 onCompleted。不知道是什么原因?。请帮帮我。

4

1 回答 1

0

除非其输入(在您的情况下为 PublishSubject)完成,否则 flatMapLatest 不会发送 onCompleted 事件。Moya 请求可以发送自己的 onCompleted 事件,但是当 flatMapLatest 合并结果时这些事件会被过滤掉。换句话说,从 PublishSubject 到数据源的长期订阅未完成,也不应该完成。(已编辑)(否则您将在第一次加载后失去刷新功能。)

我从 RxSwift Slack 频道的 Zsolt Varadi 那里得到了帮助。

这是解决方案

results = refreshTrigger
        .startWith(())
        .do(onNext: {
            execute.value = true
        })
        .flatMapLatest {
            provider
                .request(.studentSearch(query: ""))
                .retry(3)
                .observeOn(MainScheduler.instance)
                .asObservable()
                .mapJSON()
                .map { respJSON in
                    guard let studentsJsonArray = JSON(respJSON)["students"].array else {
                        throw APIError.wrongJSONParsing
                    }
                    return studentsJsonArray.map {
                        guard let students = Student.fromJSON($0) else {
                            fatalError("Invalid Student Object")
                        }
                        return students
                        } as [Student]
                }
                .map { StudentGroup(header: "Follower", items: $0) }
                .toArray()
                .catchErrorJustReturn([])
                .do(onNext: {
                    studentGroups.value = $0
                    execute.value = false
                    guard let isEmpty = studentGroups.value.first?.items.isEmpty else {
                        return
                    }
                    noResults.value = isEmpty
                })
        }
        .asDriver(onErrorJustReturn: [])
于 2017-09-27T08:16:13.050 回答