Flummox Flummox - 1 year ago 93
Android Question

Android Activity & Async SQLite communication Optimalization

Case: For the users of my app to be able to use the App even when they have no access to internet, I store the data in a SQLite Database. For different parts of the App to get at current user data I use a Singleton.
Goes like this;

Get data and store before use:
Request to server => package with data => Singleton => SQLite DB.

Show data for user:
SQLite DB => Java Classes => Singleton => Activity

Call to store the data to the SQLite DB:

DatabaseConductor dbc = new DatabaseConductor(getApplicationContext());

the DatabaseConductor is basic does this:

public class DataBaseConductor extends AsyncTask<String, String, String> {
private Context context;
private DataBaseConductorCueing returnCue = null;

for (String message : params) {
Log.d("Doing in BG", message);
switch (message) {

And yes, both the DatabaseConductor and the calling Activity use a interface for communication that stuff is done. Like so:

public interface DataBaseConductorCueing {
void processFinish(String output);

For now I use public static final Strings to check what needs to be done, and to be honest, ENUM would probably be better. I hope this code is clear to show how it works. Are there other things that might be done to optimize this?

edit 1: Integers are better then Enums.

edit 2: The design pattern for the interface is a Callback Interface, that way you don't have to register handlers.

Answer Source

Seems like subscription dbc.setReturnCue(MyActivity.this);

If you do not unsubscribe you'll get memory leaks.

You mentioned Singleton, but in the code snippet instance of DatabaseConductor.

AsyncTasks are executed serially on a single background thread (from API 11). So long running worker can block others.

How often do you hit requests? You may consider to use service.

public static final Strings to check what needs to be done

Do you have just one Activity client?

How do you solve data synchronization?

If you are really concern about optimization don't use enum.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download