I'm trying to understand what is the best approach to use when calling an async method that updates my ViewModel. Right now, let's say I have something like this:
View:
private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
//Call my ViewModel method to update the data the UI is bound to
}
ViewModel:
public async Task loadData()
{
this.Source = await loadStuffFromDatabaseAsync();
}
Now, I'm not sure which one of the following approaches should I use:
1) In my LoadState method, use:
await Task.Run(async () => { await ViewMode.loadData(); });
2) Use Task.Run without awaiting the loadData method inside the Action :
await Task.Run(() => { ViewModel.loadData(); });
3) Call my loadData method with:
await ViewModel.loadData().ConfigureAwait(false);
4) Call the loadData method without awaiting it in my View class and use Task.Run inside my loadData method:
View:
private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
ViewModel.loadData();
}
ViewModel:
public async void loadData()
{
await Task.Run(async () =>
{
this.Source = await loadStuffFromDatabaseAsync();
});
}
What are the main differences between these approaces?
Is one more efficient that the other, and should I pick one in particular?
Thanks for your help! :)
Sergio