I think I might be on the wrong track here so I’m posting this to find out.
I’m developing an android app that uses a number of rest-service requests to load its data.
I arranged my code following the n-tier pattern: domain layer (domain) / webservice access layer (dal) / business logic layer (service) / ui layer (ui)
From my activity I start an AsyncTask to load the items from the service layer. While the items are loading I show a progress dialog with a continious looping circle icon. This isn’t very user friendly, so I would like to display progress in stead via a progress indicator.
I can easily alter my dal so that it first gets a list of id’s of the items to fetch, and then fetches them one at a time, in stead of in bulk. This way I could report progress to the user, while updating the ui everytime a new item (or 2 or more) is loaded.
The problem is that I don’t know how to go about this. All the examples of AsyncTask work on the principle that everything happens within a class that inherits AsyncTask and this has to be executed from the ui thread.
Is there anyone who can provide a short code example on how I should define my AsyncTask class, and how I would be able to report progress from within the data access layer to the UI?
If I put all the code in the ui, there would be no problem, but that’s not very good practice imo.
Code example of what I want to do:
Somewhere in MainApplication I keep a list of orders:
public List<Order> orders = null;
My activity calls the AsyncTask and starts it:
OrderActivity.java
public void loadOrdersAsync() {
// Create async task instance
OrderLoaderAsyncTask loader = new OrderLoaderAsyncTask();
// Start loading
loader.doInBackground();
}
OrderLoadAsyncTask.java
public class OrderLoaderAsyncTask extends AsyncTask {
@Override
public Integer doInBackground(Context.... params) {
OrderService service = new OrderService();
MainApplication.orders = service.getAll();
}
@Override
public void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// Update progress bar
updateProgressBar(values);
}
}
OrderService.java
public class OrderService {
public List<Order> getAll() {
// Check to see if we have a cached version
// If we don't, we fetch the orders from REST webservice
OrderRepository repository = new OrderRepository();
// Get list of ID's to fetch
List<Integer> ids = new ArrayList<Integer>;
List<Order> orders = new ArrayList<Order>;
ids = repository.getIDs();
// For each id, fetch order from webservice
for (Integer id : ids) {
Order order = repository.getOneById(id);
orders.add(order);
// Report progress, but how?
}
return orders;
}
}
Now in my service I can keep track of the progress, but how do I report this progress to the UI without moving the logic to build the orderslist to my AsyncTask class? How do I report progress from within my service layer? Or even from within my repository layer?
I don’t see a way of doing that with precision, REST services are kind of request-reply, you’ll receive data only after its all loaded in data layer inside REST server.
I’ll do it with a progress bar, updating each 500mS with a Timer. Just have in mind that Timer fires another thread, so you need to update UI inside a runOnUiThread() method of your activity.
Example code: