0

I'm using MVVM and I have an app with a UITabBarController, the Main tab of which is a list of publications and the other is a Search screen where they can search for publications. Searching returns a list of publications.

Imagine that Publication A is the first item in the list on the Main tab. Now let's say the user goes to the Search tab, performs a search, and Publication A is in the search results. If the user taps Publication A, it will be marked as read and shaded certain color in the search results list. If the user goes back to the Main tab, Publication A should also be shaded to indicate it has been read.

There are plenty of MVVM tutorials on the subject but they all seem to assume one Model, one View, and one ViewModel. I believe what I need is to understand how to wire up a single Model to two Views, and two ViewModels. I have found several threads here in Software Engineering (like this one) but no real guidance or answers.

Can somebody please point me in the right direction? Thanks.

1

One way to do what you want is to post a notification when the model changes. Any ViewModel that wants to know about the change can listen for the notification and update when it is received.

  • True, but the app starts on the Main tab and the Search VM only gets instantiated when the user taps on the Search tab so it wouldn't be listening. I suppose the answer to that would be to create it on launch but is that a good practice? – Jim Jan 13 at 16:28
  • How does the Search VM get populated? It seems like you'd probably copy the relevant parts of the model into the VM at view creation time, right? In that case, it starts out synched and you can then listen for additional notifications. Or you could create it at app startup if it's not going to be a drain on resources. – user1118321 Jan 13 at 20:57

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.