Wednesday, March 20, 2013

Updating my mobile apps for Async

Xamarin recently announced support for Async in the form of an Alpha release. Time to update some code to take advantage.


Above is the change made in my service class that log's a user in. The LoginSession class was already returning a Task. I removed the ugly ContinueWith and the 2 delegates passed in. Now I just return the Task.


In my LoginViewModel I can make the changes above. Much cleaner. Note how the lambda passed to the RelayCommand is marked as Async. This is the magic that allows me to wait on the CsService.Login call. Once the Login completes I can continue where I left off in the UI thread and close the login dialog or handle any exceptions. Both the LoginComplete and LoginFailed methods are completely removed and now handled in the same order you would expect if there was no need to wait for the login to go out and make a network call (which takes time).

If you just getting into async I can recommend watching the video series by Lucian Wischik Three Essential Tips for Async