I’ve recently had the problem of trying to display a progress dialog when executing an asynchronous operation and to dismiss that progress dialog when the operation completes.
I wanted to build a way to do this that is generic to my application, so that it would work with any asynchronous operation in order to reduce duplication of writing progress dialog logic everywhere in the code.
Looking at the problem
So here is some basic pseudo-code of the problem.
Now the problem with this code is that it would have to be repeated everywhere I want to make an asynchronous call and display a progress dialog.
If I want to do this in my application every time that I make an asynchronous call, I have to put this kind of code in many places in the application.
You can also see a mixing of responsibilities here. We are handling UI related showing of a progress dialog split between an initial call and the callback. It just seems a bit dirty to do things this way.
Breaking it down
So how can we solve this problem?
Go ahead and think of some way that you might be able to solve this.
One of the best ways that I have found to solve any problem like this is to work backwards.
Let us assume that any syntax we can think of is possible, and then only change the ideal syntax if it proves to not be possible.
What do I mean by this?
Simple, let’s come up with the way we want this code to look and we will worry about implementing it later.
It would be nice to be able to execute an asynchronous method and display a progress dialog with a one-liner, like so:
If we could just do this wherever we want to make an asynchronous call and automatically have the progress dialog shown and dismissed, life would be wonderful.
Solving the problem
In order to solve this problem, we can do something very similar to a decorator pattern.
We can basically just wrap our callback method with a new callback that adds the functionality of dismissing our progress dialog.
So the idea will be that we will do the following steps in our ExecuteAsync method:
- Show the progress dialog
- Wrap our callback with one that dismisses our dialog and then executes the callback.
- Execute our asynchronous operation passing the new wrapped callback as the parameter.
Here is what this looks like in code:
This code actually looks more complicated than it really is.
You will need to understand how Action works though. If you don’t, check out this post I did explaining Action and Func.
Understanding the solution
It can be a bit hard to wrap your head around Action of type Action of type Result.
Let me translate this to make it a bit easier.
The first parameter to ExecuteAsync is a method that takes another method as a parameter (the callback,) which takes a Result object as a parameter.
Go ahead and read that over until you get it.
So, the idea here is that we are passing in the method that is going to do the callback as the first parameter to your ExecuteAsync method.
Then we are passing in our actual callback as the 2nd parameter to ExecuteAsync.
The idea is that we are splitting apart the method and the callback it takes, so that we can insert our code to handle the dialog where we need to.
Now we can just call ExecuteAsync and pass it our method and its callback and progress dialog code will automatically be handled for us.
Building on the idea
We can expand upon this concept in several ways.
One thing we could do is also apply error handling logic to the callback wrapper method. We could preprocess the result in some way before passing it to the callback. This could allow us to display an error message or even retry a number of times before executing the callback.
Another thing we could do is to change the signature of the ExecuteAsync method so that it takes a type T instead of Result; this would allow us to handle any kind of return type and method, and the ExecuteAsync method would work with just about any asynchronous method call.