0

I have a language translation app that needs to download some initial data on the first run. When the app is first launched, the first step is to get a list of currently supported languages from the server. The user then selects which they wish to install, and the rest of the tables are then downloaded.

I check if the languages are present in the local db, and if not, connect to the server and download them as JSON. The user cannot do anything until this data is retrieved from the server.

If there is no network connection, a dialog should prompt the user to go to their WIFI settings. If there is a network error with the download, another dialog would then prompt the user to retry now, or wait until later. If the download succeeds, a new Intent is launched to send them to choose the languages to install.

I have this mostly functioning (my AlertDialogs aren't showing), but the question is whether this is the proper way to accomplish this. I've currently set it up as an AsyncTask, but I've seen plenty of posts with responses yelling about how an AsyncTask should not be used when the UI depends on it. Fair enough, but an AsyncTask seems to be the recommended method for downloading data.

Is an AsyncTask the correct way to download the data, or is there a preferred alternative?

How to best deal with this on the UI, as it depends on this data? A splash screen? I'd rather not, but it seems something should be there, and I need somewhere to display the AlertDialogs if necessary.

asorenson
  • 169
  • 3
  • 14

1 Answers1

1

Is an AsyncTask the correct way to download the data, or is there a preferred alternative?

No, AsyncTask is not a good solution for networking because:

  1. You need a component from where you start this task. Activity is not a good choice because your network request will be tied to the UI and it will be hard to handle screen rotations, etc.

  2. AsyncTask works on a global serial executor by default. It will block all other async tasks in the app until it finishes. So you will have to provide your own executor to avoid that.

  3. The process level would be Background Process according to http://developer.android.com/guide/components/processes-and-threads.html With a Service you can achieve Service Process which is better.

Use Service instead. You can implement any threading you want inside. You can use a regular Thread, a ThreadPoolExecutor, a Handler, or some third party solution. Service provides you great flexibility.

Regarding your second question, take a look at material design spec first: https://www.google.com/design/spec/material-design/introduction.html

Come up with some ideas and then ask a separate question if that is still not clear.

Gennadii Saprykin
  • 4,505
  • 8
  • 31
  • 41
  • Let's forget the UI. If `AsyncTask` is not a good solution, why is it what is used in so many examples of downloading data from a server? The documentation states "This class allows to perform background operations and publish results on the UI thread," which would seem to fit this use. Further, looking at various examples of implementing a Service for downloads, such as here: [link]http://code.tutsplus.com/tutorials/android-fundamentals-downloading-data-with-services--mobile-5740, they implement an AsyncTask within the Service. Not disputing you, just confused by statements on both sides. – asorenson Jul 29 '15 at 06:51
  • According to this post: [http://stackoverflow.com/a/6957909/4527140], it sounds like AsyncTask exactly fits the bill. The downloading of the languages is a one-off task, on the first run. Also, in this example on basic networking operations, an AsyncTask is used: [http://developer.android.com/training/basics/network-ops/connecting.html ] – asorenson Jul 29 '15 at 07:00
  • I think `Service` is more convenient solution for networking, and yes, you can use `AsyncTask` in your `Service` if you want (but be careful about #2). I personally don't like using `AsyncTask`s for networking without a `Service` because network connection might be slow and the `AsyncTask` might take about a minute to perform. If you don't use a `Service` in this case and just start `AsyncTask` from your `Activity`, how would you update your UI then? User might change the screen orientation and `Activity` will be recreated.. You need to handle that case which is pain.. – Gennadii Saprykin Jul 29 '15 at 14:57