Checking that async data load compete before accessing data

45 views Asked by At

I'm using MVVMlight SimpleIoC container, particularly to store quite a large volume of data, which is accessible throughout the application. When the application is started, I launch an asynchronous process, which prepares the data.

private MethodOfOriginalThread()
{
    (new Thread(this.LoadRegistry)).Start();
}

private void LoadRegistry()
{
    int meetingId = SimpleIoc.Default.GetInstance<MeetingDetails>().MeetingId;
    List<Shareholder> registry = this.dataService.SearchRegistry(meetingId, string.Empty);
    SimpleIoc.Default.Register(() => registry);
}

The process of obtaining the data takes approximately 10 seconds. After the process is complete, I save the data to container, as seen in the code above.

Later from a different viewModel I'm accessing this data:

this.shareholders =
    new NotifyObservableCollection<Shareholder>(
    SimpleIoc.Default.GetInstance<List<Shareholder>>()
);            

The problem arises when this access is made before the actual load completes. Since the viemodels are very loosely coupled, I cannot refer to one from another. Sending messages may result in hyge amount of code, since these results are widely used in the application, and each viewmodel should have the message handling code. How can I check and wait, whether the data has been successfully loaded? An endless loop, which waits for required object in the container, sounds halfway ok, since the operation should eventually be completed, but I'm worried that this is not the most elegant solution. And also I need to let the user have some feedback that the data is still being loaded, so he waits patiently, and does not press too many buttons. This is quite challenging with an endless loop and frozen application.

0

There are 0 answers