Jakob Kristensen
Jakob Kristensen

Reputation: 1757

Seed GreenDao database on Android

I'm using GreenDAO for an Android project for the first time, and was wondering how to seed the database for first-time users? Say for instance i have a table and want 5 rows inserted on behalf of the user.

Also, i might add new tables in future updates and seed data into those as well, but still want to have the five rows inserted into the first table, even though the user is installing a newer version of the scheme.

My initial idea was to do it in my App.onCreate() method, and then set a flag in SharedPreferences as whether or not the seed has been made already, but it bugs me that i can't find a more pragmatic approach to this.

Any help appreciated, thanks!

Upvotes: 0

Views: 412

Answers (1)

BehzadBx
BehzadBx

Reputation: 67

I had the same problem and searched the web and the documentation of GreenDAO but didn't find anything reliable.

So I wrote a code to run in the first run of the app. To do so I needed to check if it's the first time that my app is launched. For doing that I recommend this answer. You can see the code from that answer here:

public static void checkFirstRun(Context context) {

    final String PREFS_NAME = "TickTockPrefs";
    final String PREF_VERSION_CODE_KEY = "version_code";
    final int DOESNT_EXIST = -1;

    // Get current version code
    int currentVersionCode = 0;

    try {
        currentVersionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
    } catch (android.content.pm.PackageManager.NameNotFoundException e) {
        // handle exception
        e.printStackTrace();
        return;
    }

    // Get saved version code
    SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    int savedVersionCode = prefs.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST);

    // Check for first run or upgrade
    if (currentVersionCode == savedVersionCode) {

        // This is just a normal run
        return;

    } else if (savedVersionCode == DOESNT_EXIST) {

        // TODO This is a new install (or the user cleared the shared preferences)
        seed(context);

    } else if (currentVersionCode > savedVersionCode) {

        // TODO This is an upgrade

    }

    // Update the shared preferences with the current version code
    prefs.edit().putInt(PREF_VERSION_CODE_KEY, currentVersionCode).apply();

}

And inside the seed method you can write whatever you want to insert. For example say I have a "Person" entity that I want to prepopulate with data:

public static void seed(Context context) {

    DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "your-db", null);
    SQLiteDatabase db = helper.getWritableDatabase();
    DaoMaster daoMaster = new DaoMaster(db);
    DaoSession daoSession = daoMaster.newSession();

    Person person = new Person();
    person.setName("Jason");
    person.setFamily("Bourne");

    PersonDao personDao = daoSession.getPersonDao();
    personDao.insert(person);
}

Note that if you want to insert a List of entities use insertInTx() method instead of insert(). You can see the difference here.

I know this is different than ORM seed method but it seems there's no other alternatives except you manipulate greenDAO code yourself.

Upvotes: 1

Related Questions