For implementing MVVM with a table view controller, it is often to have a single parent view model and a bunch of child view models for each cell. Let's say each cell has a like button, and now the user taps one of the like buttons.
After searching through stack overflow, I see three possible ways to handle the flow:
The tapped action is sent to the child view model, the child view model handles the like action internally.
The tapped action is sent to the child view model, the child view models pass the intent to the parent view model, which handles the like action.
The tapped action is sent to the table view controller (using cell delegate or closure), the table view controller then pass the tapped action to the parent view model.
Personally, I prefer the 2nd and 3rd approach. What confuses me is that in the 3rd approach, the child view model is only responsible for the output (presenting data), but not for the input (handling interactions). Instead, the parent view model is responsible for handling the child view's interaction. Because normally a view model will handle both for its view.
Which approach is better? Or are there better ways to achieve the same goal?
Any advise would be helpful.
The first question I would ask myself in this situation is whether any state in the view controller has to change based on inputs into the cell. If so, then option 2 is the best choice (assuming RxSwift.) If you are using delegates/closures instead of Observables then option 3 is handy to reduce the number of delegates required.
From your description, it doesn't sound like the view controller's state needs to be updated when the user taps the button, so option 1 sounds like the best.
Here is how I would likely implement it using my CLE architecture... You can think of the
connectfunctions as view models.