Skip to content

Instantly share code, notes, and snippets.

@StephenCleary
Created May 8, 2014 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StephenCleary/9eca76bda1fb2b2fe614 to your computer and use it in GitHub Desktop.
Save StephenCleary/9eca76bda1fb2b2fe614 to your computer and use it in GitHub Desktop.
It is possible to use any kind of async technique with an async MVVM application. I have found that instead of using the Interleaved operator, it's usually better to write a separate higher-level async method and then use Task.WhenAll over that. IMO the code ends up being more clear.
That said, you should be able to use Interleaved (or Jon Skeet's OrderByCompletion, or my own OrderByCompletion) with AsyncCommand by adding the results to an ObservableCollection:
myAsyncCommand = new AsyncCommand<object>(async () =>
{
foreach (var result in GetDataAsync().Interleaved())
myObservableCollection.Add(await await result);
return null;
});
As each task completes, its result is added to the ObservableCollection, which updates immediately. You'll know that all the tasks have completed when myAsyncCommand.Execution.IsCompleted/IsNotCompleted changes.
This is a simple example that may not handle errors the way you want. Any exceptions will propagate through myAsyncCommand.Execution.IsFaulted/ErrorMessage, but will not cancel any remaining requests or modify the ObservableCollection at all. You can customize the error handling by putting a try/catch in the foreach loop.
@robowarrior2
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment