Yesterday I took the time to introduce MadProps.MvvmLight into Halfwit, so I could replace some of the convoluted asynchronous code with simple coroutines. Let's take a look at one example.

When you choose to "block" a user in Halfwit, a couple of things need to happen:

You're prompted with a dialog box asking you to confirm your action

If you click "No", we don't do anything

If you answered positively, we make an asynchronous call to Twitter to block the user

If the call didn't succeed, we report the error and stop work

If the call succeeded, we tell you that the user is now blocked and remove any tweets of theirs from your timestream.

A lot of those steps require asynchronous work. Either explicitly asynchronous, like the call to Twitter, or asynchronous as far as the ViewModel is concerned, like the showing of the dialog box. Previously there were two or three different methods involved, since each asynchronous step needed to provide a callback method for the next step.

Coroutines have simplified this procedure down to a single, sequential-looking method.

Each "activity" is yielded from our method and processed in turn, and control is not returned to the method until that activitiy completes. So we can show our message box and wait for it to return, then ask Twitter to block the user and wait for that to return, and finally take some action based on the response from Twitter.

Take it from me: This is a big improvement over the series of methods that used to make up this procedure. Callbacks inside callbacks is no way to code.

If you haven't already played with the MVVM Light coroutine support in MadProps.MvvmLight, go ahead and download it! If you grab the complete source there's a sample project to try out.