Stop toggling IsBusy with NotifyTask!

Introduction

When using the MVVM pattern, it is quite common to create a BaseViewModel and then, as a first step, add a property called “IsBusy”. This property is then toggled at the begin / end of async operations, more usually REST calls to a server. And the View reacts showing an activity indicator.

If this sounds familiar to you, don’t worry, as there is nothing wrong with this approach. However, it can be improved a lot by using the super cool library Nito.Mvvm.Async from Stephen Cleary, which contains a magical class called NotifyTask.

Concept

The Nito.Mvvm.Async package contains a set of helpers when building applications that use the MVVM pattern.

Within that toolkit we can find a class named NotifyTask. It provides us with an object that can watch for Task states and then raise property-changed notifications for it. This means we can then listen to the task’s property changes from the View and even bind things to it!

The properties this class exposes are the following:

As you can see, there’s a lot of stuff in there. Even a string property called ErrorMessage that you can subscribe to!

 

Usage

The way you would normally use NotifyTask is by having a public property in your ViewModel, something like this:

The initialization of the object happens in line 14. There are multiple constructors, better to use the one that takes a Func<Task> ;).

That’s it! All you have to do now is to create bindings in your Views for it. For example:

  • In case of Android + MvvmCross, you could do something like:

  • In case of iOS + MvvmCross, the same binding would look this way:

  • Now on Xamarin.Forms .xaml it would be something like:

 

Advantages

Using this library instead of a simple bool property might sound like an overkill. But it isn’t, belive me. These are the main benefits I can enumerate so far:

  • NotifyTask acts as a container for exceptions. If anything goes wrong with the async operation, your app won’t crash and the exception will be stored in the NotifyTask object. This sets your code free from try/catch/finally blocks in your ViewModels and also makes possible for the UI to listen for the Exception property and react to its changes.
  • All changes and toggling of properties now happens automatically, you will never miss a line “IsBusy = false” that can block your app! This makes of course your code safer.
  • It provides you with a way to initialize your ViewModels async, from a sync context. No need to mark any method as “async void” nor to use “Task.Run(() => MyMethodAsync());” anymore!
  • If you create a NotifyTask in your BaseViewModel and then use that one property to initialize your ViewModels, you can now think about your Views as state machines with different states: Loading, Loaded, Failed, …
  • Lots of new properties to play with! Possibilities are now endless.

 

 

Further reading: Async Programming: Patterns for Asynchronous MVVM Applications: Data Binding

 

3 comments on “Stop toggling IsBusy with NotifyTask!”

  1. Mariano says:

    Great article!

  2. Great Article! :))) Really interesting!

Leave a Reply

Your email address will not be published. Required fields are marked *