[bug] ORMlite - SQLCipher Lost Data After Force Close / Kill Apps From Recent Apps

41 views
Skip to first unread message

Muhammad Kailani Ridwan

unread,
Nov 14, 2018, 1:29:50 PM11/14/18
to ORMLite Android Users
i want to implement SQLChiper with ORMLite this library, i want to setup follow us : 

build.gradle
    implementation 'com.j256.ormlite.cipher:ormlite-sqlcipher:1.2@aar'
    implementation
'com.j256.ormlite:ormlite-core:5.1'
    implementation
'com.j256.ormlite:ormlite-android:5.1'
    implementation
'net.zetetic:android-database-sqlcipher:3.5.9@aar'


DatabaseHelper.java
package com.devleoper.id.utils;


import android.content.Context;
import android.database.SQLException;
import android.util.Log;


import com.devleoper.id.BuildConfig;
import com.devleoper.id.model.db.Profile;
import com.j256.ormlite.cipher.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;


import net.sqlcipher.database.SQLiteDatabase;


import java.io.File;
import java.io.IOException;




/**
 * Created by Developer on 11/14/18.
 */



public class DatabaseHelper extends OrmLiteSqliteOpenHelper {


   
private static final String DATABASE_NAME = "ut_v2_secure.db";
   
private static final String DATABASE_PATH = String.format("/data/data/%s/databases/", BuildConfig.APPLICATION_ID);
   
private static final int DATABASE_VERSION = 1;
   
private static final String PASSWORD = "Testing Apps";


   
private static DatabaseHelper sInstance;


   
private Dao<Profile, Integer> profileDao;


   
public DatabaseHelper(Context context) {
       
super(context, DATABASE_NAME, null, DATABASE_VERSION);


       
// Decrypt Database
        getWritableDatabase
(getPassword());
   
}


   
@Override
   
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
       
try {


            setNullDaoObject
();


           
TableUtils.createTableIfNotExists(connectionSource, Profile.class);






       
} catch (SQLException | java.sql.SQLException e) {
           
LogUtil.debug("Could not create new table: " + e);
       
}
   
}


   
private void setNullDaoObject() {
        profileDao
= null;
   
}


   
@Override
   
public void onUpgrade(net.sqlcipher.database.SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
       
Log.d("DatabaseHelper", "onUpgrade: " + oldVersion + " to " + newVersion);


       
try {
            onCreate
(db, connectionSource);
       
} catch (SQLException | java.sql.SQLException e) {
           
LogUtil.debug("Could not create new table: " + e);
       
}
   
}


   
@Override
   
protected String getPassword() {
       
return PASSWORD;
   
}


   
public static synchronized DatabaseHelper getInstance(Context context) {
       
if (sInstance == null) {
            sInstance
= new DatabaseHelper(context.getApplicationContext());
       
}


       
return sInstance;
   
}
   
public Dao<Profile, Integer> getProfileDao() {
       
if (profileDao == null) {
           
try {
                profileDao
= getDao(Profile.class);
           
} catch (java.sql.SQLException e) {
               
LogUtil.debug("Failed get profileDao: " + e.getMessage());
           
}
       
}
       
return profileDao;
   
}


   
@Override
   
public void close() {
        setNullDaoObject
();


       
super.close();
   
}


}



Initialize Library  SQLCipher + ORMlite on App.java 

App.java


package com.devleoper.id;


import android.app.Activity;
import android.app.Application;
import android.os.Bundle;


import net.sqlcipher.database.SQLiteDatabase;


/**
 * Created by developer on 11/14/18.
 */

public class App extends Application{


   
@Override
   
public void onCreate() {
       
super.onCreate();


       
// Iniitalize SQL Chiper
       
SQLiteDatabase.loadLibs(this);
       
   
}


}





i want call helper database on BaseActivity for retrieve data

BaseActivity.java


public abstract class BaseActivity extends AppCompatActivity {
   
   
public DatabaseHelper databaseHelper = null;


   
@Override
   
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
       
super.onCreate(savedInstanceState, persistentState);
   
}


   
/**
     * You'll need this in your class to get the helper from the manager once per class.
     */

   
public DatabaseHelper getDatabaseHelper() {
       
if (databaseHelper == null) {
//            databaseHelper = OpenHelperManager.getHelper(this, DatabaseHelper.class);
            databaseHelper
= DatabaseHelper.getInstance(this);
       
}
       
return databaseHelper;
   
}




   
/**
     * You'll need this in your class to release the helper when done.
     */

   
public void releaseDatabaseHelper() {
       
if (databaseHelper != null) {
           
OpenHelperManager.releaseHelper();
            databaseHelper
= null;
       
}
   
}




   
@Override
   
protected void onDestroy() {
       
super.onDestroy();
       
// Dest
        releaseDatabaseHelper
();
   
}
}




MainActivity.class


public class MainActivity extends BaseActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView
(R.layout.activity_splash_screen);


       
try {
           
// Get Data List Profile
           
List<Profile> profiles = getBaseActivity().getDatabaseHelper().getProfileDao().queryForAll();
           
if (profiles != null && profiles.size() > 0) {
                profile
= profiles.get(0);
           
}
       
} catch (SQLException e) {
            e
.printStackTrace();
       
}        
   
}


   
@Override
   
public void onBackPressed() {
   
}






   
@Override
   
protected void onDestroy() {
       
super.onDestroy();
   
}


}




** Problem
I found the problem retrieve data as follows :
1. when I retrieve data for the first time the data application can be on the MainActivity.java, **List <Profile> data size (100 data) ** from Library Ormlite-SQLChiper. 
2. then I press the home button on the application, then I press the recent apps button, to do the force / Kill apps. Then I reopen the application.
4. When I retrive the Data List <Profile> on MainActivity.java, **the data size is (0 data)**
5. the data is lost.

i want to use for function retrieve data is : 
getBaseActivity().getDatabaseHelper().getProfileDao().queryForAll()


is there a solution to this problem ?, is my coding wrong? 
or incorrect to retrieve when the application is force close from recent apps and reopen. 

* Noted : 
above the problem. if when user back to home screen, and user press button recent apps ->swipe for kill application -> re-open Application -> retrieve data -> data size 0 (Profile), if not Kill Recent Apps Data Size 100 (Profile)

Expeted Retrieve List Data:  

List<Profile> profileList = getBaseActivity().getDatabaseHelper().getProfileDao().queryForAll(); 

profileList.size() = 100 Data

Actualy Retrieve List Data (Kill Recent Apps - reopen apps) : 
profileList.size() = 0 Data  (Data is lost)

Library Reference :
https://github.com/Andoctorey/ormlite-sqlcipher

Thank you.
Reply all
Reply to author
Forward
0 new messages