Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tribushevsky opened this issue Jan 30, 2020 · 1 comment

Comments

@tribushevsky
Copy link

tribushevsky commented Jan 30, 2020

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?

@tribushevsky tribushevsky changed the title How to use collectionView.rx.modelSelected method for multiple model How to use collectionView.rx.modelSelected method for multiple models Jan 31, 2020
@lordzsolt
Copy link
Member

lordzsolt commented May 10, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants