简体   繁体   中英

How we can reslove Greendao DB close error?

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$Runn ableAdapter.call(Executors.java:428) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at com.google.android.gms.internal.zzato$zzd.run(Unknown Source)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM