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:
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.
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.