tjay
tjay

Reputation: 71

SQLite usage from activity and service

I have created a databaseprovider class which uses single instance of db object. Object is created in main activity and closed onDestroy method. This seems ok (but get some errors such as: db already closed or db is not open on some users devices that I cannot simulate).

I want to add a service to the application for the content download and this service can run with scheduler which make me think about single instance of db object will not work. Should I use another object for the service, will it result consistency problems? Can you kindly advice what would be the best way?

Databaseprovider class exm:

public class DatabaseProvider {
private static DatabaseHelper helperWriter;
public static SQLiteDatabase db_global;
public DatabaseProvider(Context c) {
    helperWriter = DatabaseHelper.getHelper(c, true);
}

private static SQLiteDatabase getDB() {     

    if(db_global == null)           
        db_global = helperWriter.getWritableDatabase();
    else if(!db_global.isOpen()) {
        try {
            db_global.close();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        db_global = helperWriter.getWritableDatabase();
    }

    return db_global;
}

public String GetVersion() {
    SQLiteDatabase db = getDB();
    Cursor c = db.query(DatabaseHelper.PARAMETER_TABLE_NAME, new String[] {"VALUE"}, "KEY='Version'", null, null,null,null);
    String version = "";
    if(c.moveToNext())
    {
        version = c.getString(0);           
    }
    else
        version = "0";
    c.close();

    return version;
}

public long UpdateVersion(String value) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(DatabaseHelper.PARAMETER_COLUMN_VALUE, value);
    SQLiteDatabase db = getDB();   
    long r = db.update(DatabaseHelper.PARAMETER_TABLE_NAME, initialValues, "KEY='Version'", null);
    if(r <= 0)
        r = helperWriter.AddParameter(db, "Version", value);
    //db.close();   
    return r;
}   

public void CloseDB() {
    if (db_global != null)
        db_global.close();
    db_global = null;
    helperWriter.close();
}
 }

Upvotes: 7

Views: 149

Answers (2)

donfede
donfede

Reputation: 734

I see a couple questions:

A)

(but get some errors such as: db already closed or db is not open on some users devices that I cannot simulate). ... Start an activity, then update content and some db operations in AsyncTask. While update is in progress go back and start the same activity again.

To work around these errors have you considered using a [Loader][1]? It's a callback based framework around ContentProviders.


B)

add a service to the application for the content download and this service can run with scheduler which make me think about single instance of db object will not work. Should I use another object for the service, will it result consistency problems?

This post by @commonsware from this website, suggests not to use Service for long running tasks. Instead the AlarmManager is suggested. I've only worked with short running services (for audio IO) myself.

Upvotes: 0

Joe Plante
Joe Plante

Reputation: 6368

Not sure if this will help, but...

you can't rely on onDestroy() in case the app crashes. Android may also keep your app in RAM, even if you exit it. Also, your main activity may get destroyed while the app is getting used if you are on a subactivity. It can also get recreated.

Sometimes it's better to have calls that open the DB, does stuff to it, and then closes it within the same function. If you are using a service, it may actually help things. I also am not sure if you should have a situation where a DB can be opened and/or accessed from a variety to different places at once without some management code

Upvotes: 1

Related Questions