-
Notifications
You must be signed in to change notification settings - Fork 114
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
Best practices regarding shared properties between ViewModels #237
Comments
Questions like this are probably generally better asked on StackOverflow with the tag AnswerI would generally prefer something closer to Option 1, because of possible threading or race condition issues. Your instincts about that are probably correct; sharing mutable state across multiple different places can cause lots of headaches later. It's hard to track and even harder to debug. 9 months from now when you are trying to figure out why your With functional reactive code, try to make your data flow explicit and read in an almost human readable way. Passing one property into the initializer of another view model isn't clear, you now have to look inside of that view model and see whether it's sharing that property, observing it internally, or maybe just using it to get an initial value and not observing it at all. Instead, having a consistent pattern you follow for where you do all your bindings is better. That could be in some service layer, in your view controller's But it's important to recognize that your two options actually currently have a functional difference in behavior. Option 1In this case, if the In order to have it update in both directions, you would want to use Also, for simplicity, you can use self.viewModel.selectedDay.selectedDay.bind(to: daysViewModel.selectedDay)
// or for bidirectional binding:
self.viewModel.selectedDay.bidirectionalBind(to: daysViewModel.selectedDay) Option 2If you want a bidirectional binding, Option 2 does that already. Sharing one property means that if it updates in either place, it will be updated in both. Extra: Read-Only PropertiesIn your class DayViewModel: BaseViewModel {
private let _selectedDay: Property<Date>
let selectedDay: AnyProperty<Date> { _selectedDay.readOnlyView }
func switched(to dayNumber: Int) {
_selectedDay.value = _selectedDay.value.dayInSameWeek(for: dayNumber)
}
} Hope all this helps! Good luck! |
Thanks, this really helped. Especially the I'm going to stick with passing through the values using bind instead of passing the property, so option 1. |
First of all my apologies if architectural questions are not supposed to be asked in the issue tracker but I couldn't find a satisfying answer anywhere else.
In my particular case I have the following two ViewModels:
I'm wondering what's the best approach. I could either monitor that property like this:
Or, I pass in the property when I construct the
DayViewModel
:On one hand I think having less state is always better, on the other hand the properties might have different life-cycles and I can also imagine the same property shared over multiple view models could cause more problems with threading etcetera.
The text was updated successfully, but these errors were encountered: