Activity and Background Service Access to SQLite Database Activity and Background Service Access to SQLite Database sqlite sqlite

Activity and Background Service Access to SQLite Database


This man Kevin is a legend: http://touchlabblog.tumblr.com/post/24474750219/single-sqlite-connection. Thank you so much.

On that link he shares his ridiculously simple solution:

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {private static DatabaseHelper instance;    public static synchronized DatabaseHelper getHelper(Context context)    {        if (instance == null)            instance = new DatabaseHelper(context);        return instance;    }    //Other stuff... } 

Then in my SQLite class I changed my code to look like this:

public BlacklistWordDataSource(Context context) {    dbHelper = MySQLiteHelper.getHelper(context);}


at onCreat in the activity put datasource.open();then do what ever you wantand at the end of actevity put this to close :

  /** Call after close activity */@Overrideprotected void onStop() {    super.onStop();    //Close dataSource    datasource.close();}