1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2025-01-03 21:55:13 +00:00

Check database context

This commit is contained in:
M66B 2021-04-25 16:13:26 +02:00
parent 5c2d6866ef
commit 8d0b2ba14c

View file

@ -110,6 +110,8 @@ public abstract class DB extends RoomDatabase {
public abstract DaoLog log(); public abstract DaoLog log();
private static Context sContext;
private static int sPid;
private static DB sInstance; private static DB sInstance;
private static final String DB_NAME = "fairemail"; private static final String DB_NAME = "fairemail";
@ -302,16 +304,28 @@ public abstract class DB extends RoomDatabase {
} }
public static synchronized DB getInstance(Context context) { public static synchronized DB getInstance(Context context) {
if (sInstance == null) { Context acontext = context.getApplicationContext();
Log.i("Creating database instance"); if (sInstance != null &&
Context acontext = context.getApplicationContext(); sContext != null && !sContext.equals(acontext))
try {
Log.e("Old database instance pid=" + android.os.Process.myPid() + "/" + sPid);
sInstance.close();
sInstance = null;
} catch (Throwable ex) {
Log.e(ex);
}
sContext = acontext;
sPid = android.os.Process.myPid();
sInstance = migrate(acontext, getBuilder(acontext)).build(); if (sInstance == null) {
Log.i("Creating database instance pid=" + sPid);
sInstance = migrate(sContext, getBuilder(sContext)).build();
sInstance.getQueryExecutor().execute(new Runnable() { sInstance.getQueryExecutor().execute(new Runnable() {
@Override @Override
public void run() { public void run() {
checkEmergencyBackup(acontext); checkEmergencyBackup(sContext);
} }
}); });