AugustJee
AugustJee

Reputation: 11

Observable<Observable<T>> to Observable<T> rxswift

Hi I am having hard time trying to figure this out. There is some bunch of other problem.

Can anyone help me making Observable<[Observable<CellModel>] to Observable<[CellModel]>?

the outcome for the request(categoryId: $0) was Observable<[Observable<CellModel>?]>

enter image description here

  selectCategory
        .do(onNext: { _ in output.cells.onNext([.init(cellID: kSkeletonTableID)]) })
        .flatMap { [unowned self] in request(categoryId: $0) }
       // .showIndicator()
        .trackError(errorTracker)
        .compactMap {
            var cellModel:[CellModel] = $0.flatMap { $0 }
            return cellModel
        }
        .bind(to: output.cells)
        .disposed(by: disposeBag)

Upvotes: 1

Views: 268

Answers (1)

Daniel T.
Daniel T.

Reputation: 33979

That's a pretty tortured type that you are returning from your request(categoryId:). The real fix is there. However, the answer to this question is:

selectCategory
    .do(onNext: { _ in output.cells.onNext([.init(cellID: kSkeletonTableID)]) })
    .flatMap { request(categoryId: $0).flatMap { Observable.combineLatest($0.compactMap { $0 }) } }
    // .showIndicator()
    .trackError(errorTracker)
    .bind(to: output.cells)
    .disposed(by: disposeBag)

You might want to consider one of the other variations of flatMap depending on the expected output. See this article for details: RxSwift's Many Faces of FlatMap

Upvotes: 1

Related Questions