RxSwiftCommunity / RxDataSources

UITableView and UICollectionView Data Sources for RxSwift (sections, animated updates, editing ...)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use collectionView.rx.modelSelected method for multiple models

tribushevsky opened this issue · comments

Hi there! I implemented the multiple section model for my collectionView dataSource, as described in the following pull request: #32.
Also, I added the following code for handling cells selection:

Screen Shot 2020-01-30 at 2 20 43 PM

But I can handle a number of next events only when tap on the only one cell, when I tap on other cells nothing is not happening. It happens because each of the other sequences produced the error and was disposed. Please check the log below.

Screen Shot 2020-01-30 at 2 20 56 PM

How can I prevent the disposing of other sequences?

Your models need to be homogenous. Either be of the same type or inherit from the same protocol or base class.

Usually, I use an enum with associated values for this. For your case, it would look like:

enum DashboardCellViewModel {
    case goal(viewModel: WTDashboardGoalViewModel)
    case water(viewModel: WTDashboardWaterViewModel)
    case weight(viewModel: WTDashboardWeightViewModel)
    case reminder(viewModel: WTDashboardReminderViewModel)
}

collectionView.rx.modelSelected(DashboardCellViewModel.self).subscribe(onNext: { viewModel in
     switch viewModel {
          case .goal(let viewModel): // Goal selecteed
          case .water(let viewModel): // Water selected
          case .weight(let viewModel): // Weight selected
          case .reminder(let viewModel): // Reminder selected
     }
}

NOT RECOMMENDED: Or you can pass Any.self to collectionView.rx.modelSelected() and switch on the type.