私は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 つのサブスクリプションが取得されます。
しかし、なぜそれは完了していないのですか?