How we can reslove Greendao DB close error?

277 Views Asked by At

When we can create greendao application We didn't get any default method to close the greendao db So how we can close the db on Application class

because we add below code on Application class

static class YourAsyncTask extends AsyncTask {

    @Override
    protected String doInBackground(Void... unused) {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(mContext, ENCRYPTED ? "otp-db-encrypted" : "otp-db");
        Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
        daoSession = new DaoMaster(db).newSession();
        return "";
    }
}

And we can use daoSession object to whole application how we can get solution for below error

Finalizing a Cursor that has not been deactivated or closed. database = /data/user/0/com.onetouchprotect/databases/google_app_measurement_local.db, table = null, query = select count(1) from messages android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here at android.database.sqlite.SQLiteCursor.(SQLiteCursor.java:98) at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:50) at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1618) at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1557) at com.google.android.gms.internal.zzatg.zza(Unknown Source) at com.google.android.gms.internal.zzatg.zza(Unknown Source) at com.google.android.gms.internal.zzatw.zzc(Unknown Source) at com.google.android.gms.internal.zzatu.zzb(Unknown Source) at com.google.android.gms.internal.zzatu.zza(Unknown Source) at com.google.android.gms.internal.zzatu$4.run(Unknown Source) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at com.google.android.gms.internal.zzato$zzd.run(Unknown Source)

1

There are 1 best solutions below

0
On

It's advisable to have a separate class that is responsible for giving you the session variable. Guide you with DaoExample in the file App.java