0

私はRxSwiftが初めてで、次の状況に遭遇しました:

self.viewModel!.country
        .flatMapLatest { country -> Observable<City> in
            country!.cities!.toObservable()
        }
        .map { city -> SectionModel<String, String> in
            SectionModel(model: city.name!, items: city.locations!)
        }
        .toArray()
        .bindTo(self.tableView.rx_itemsWithDataSource(dataSource))
        .addDisposableTo(self.disposeBag)

toArray() は何もしないように思えますが、その理由はわかりません。一方、このコードは私が望むことを行います。前のコードが同じように機能しない理由を知りたいです。

self.viewModel!.country
        .flatMapLatest { country -> Observable<[SectionModel<String, String>]> in
            var models = [SectionModel<String, String>]()
            for city in country!.cities! {
                models.append(SectionModel(model: city.name!, items: city.locations!))
            }
            return Observable.just(models)
        }
        .bindTo(self.tableView.rx_itemsWithDataSource(dataSource))
        .addDisposableTo(self.disposeBag)

前もって感謝します。

編集:

ビューモデルの実装は次のとおりです。

class LocationViewModel {

    let country = BehaviorSubject<Country?>(value: nil)

}

Country には、City の配列であるプロパティ「cities」があります。

@solidcell toArray() の前後に debug() を配置すると、debug() ごとに 1 つ、および toArray() debug() の前にのみ配列項目ごとに 1 つの次のイベントの 2 つのサブスクリプションが取得されます。

しかし、なぜそれは完了していないのですか?

4

1 に答える 1

0

おそらくcountry!.cities一部の国では終了しないためです。 が完了toArray()するまで要素を放出しません。Observableコンプリートが完了Observableすると、すべての要素が .xml として 1 つの要素にパッケージ化されますArray。ただし、これがあなたの問題かどうかはわかりません。あなたがcountry!.cities.

そこにを入れてみて、.debug()都市の Observable が完了していないことが原因であるかどうかを確認できるかどうかを確認してください。

于 2016-05-24T20:54:48.567 に答える