MVVM Collections loading asynchrounously
I have a view model with 5 collections.
All of them load asynchronously.
I need to load the main entity from a service and set the selected value of each collection based of the value in the entity. How do I wait until all the collections have been loaded before loading the entity and set开发者_如何学Goting the selected values?
I feel like I am missing something.
When your MainViewModel
(the one with the "main entity") completes loading set its SelectedRecord
property. Setting this property should raise an event or broadcast a message that your other ViewModels can listen to.
When the other ViewModels receive the message from the main view model, set an IsDirty
flag to true. The other ViewModels will then set their SelectedData
properties whenever they complete loading (in the case that the MainViewModel finished first) or whenever IsDirty
is set to true -- whichever comes first
If they are quick loading you could just chain the loads calling the next load on the callback from the previous.
If not then leave all your collections at null/nothing (many people initialize them in the constructor) and in the load callback of each, Instantiate the collection before you fill it, leaving it empty if no entities/results are returned. Then call a function that checks that none of the collections are null/nothing, and calls the inotify for all of them at once.
A similar approach that would allow you to instantiate your collections before they are loaded, would be to create an attached property "isLoaded" for your collections and use that.
精彩评论