If I understand correctly, when working with DB, I have to do as follows
DaoMaster.OpenHelper helper = new DaoMaster.OpenHelper(this, "test-db", null) {
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
};
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
But if I try to do this in classes that aren’t extending activity or service, I simply couln’t pass their context.
What is right approach to open my DB? Where should it be done?
If you can provide some tutorial links besides official greendao (I couldn’t find the answer there), it would be great.
provide custom application object and use its context (so Application Context).
in your AndroidManifest file, provide a class that extends Application.
MyApp class looks like this:
so now whenever you need context in your app, you can call
MyApp.getInstance. As long as you call it after MyApp’sonCreateis called, you will be safe. Keep in mind thatinstanceis your application context so it will be alive as long as your app is alive. (e.g. no danger of leaking)