From 8d0b2ba14c21a60862e6ef64aa297e9f58025da1 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 25 Apr 2021 16:13:26 +0200 Subject: [PATCH] Check database context --- app/src/main/java/eu/faircode/email/DB.java | 22 +++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DB.java b/app/src/main/java/eu/faircode/email/DB.java index a5f5a1a36d..3789f6d4a4 100644 --- a/app/src/main/java/eu/faircode/email/DB.java +++ b/app/src/main/java/eu/faircode/email/DB.java @@ -110,6 +110,8 @@ public abstract class DB extends RoomDatabase { public abstract DaoLog log(); + private static Context sContext; + private static int sPid; private static DB sInstance; private static final String DB_NAME = "fairemail"; @@ -302,16 +304,28 @@ public abstract class DB extends RoomDatabase { } public static synchronized DB getInstance(Context context) { + Context acontext = context.getApplicationContext(); + if (sInstance != null && + 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(); + if (sInstance == null) { - Log.i("Creating database instance"); - Context acontext = context.getApplicationContext(); + Log.i("Creating database instance pid=" + sPid); - sInstance = migrate(acontext, getBuilder(acontext)).build(); + sInstance = migrate(sContext, getBuilder(sContext)).build(); sInstance.getQueryExecutor().execute(new Runnable() { @Override public void run() { - checkEmergencyBackup(acontext); + checkEmergencyBackup(sContext); } });