From e7ce5c669e03b1c89a83416834e858398e39b31f Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sat, 7 Apr 2018 16:21:57 +0200 Subject: [PATCH] Using Bill Pugh Singleton Implementation --- .../antennapod/core/storage/PodDBAdapter.java | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java index 99f5a466f..e82252310 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java @@ -308,26 +308,22 @@ public class PodDBAdapter { KEY_CONTENT_ENCODED, KEY_FEED}; private static Context context; - private static PodDBHelper dbHelper; private static volatile SQLiteDatabase db; private static int counter = 0; - private static PodDBAdapter instance; public static void init(Context context) { PodDBAdapter.context = context.getApplicationContext(); } - private static class PodDBHelperholder { - public static final PodDBHelper dbHelper = new PodDBHelper(PodDBAdapter.context, DATABASE_NAME, null); + // Bill Pugh Singleton Implementation + private static class SingletonHolder { + private static final PodDBHelper dbHelper = new PodDBHelper(PodDBAdapter.context, DATABASE_NAME, null); + private static final PodDBAdapter dbAdapter = new PodDBAdapter(); } public static PodDBAdapter getInstance() { - if (instance == null) { - instance = new PodDBAdapter(); - } - dbHelper = PodDBHelperholder.dbHelper; - return instance; + return SingletonHolder.dbAdapter; } private PodDBAdapter() { @@ -346,11 +342,11 @@ public class PodDBAdapter { private SQLiteDatabase openDb() { SQLiteDatabase newDb; try { - newDb = dbHelper.getWritableDatabase(); + newDb = SingletonHolder.dbHelper.getWritableDatabase(); newDb.enableWriteAheadLogging(); } catch (SQLException ex) { Log.e(TAG, Log.getStackTraceString(ex)); - newDb = dbHelper.getReadableDatabase(); + newDb = SingletonHolder.dbHelper.getReadableDatabase(); } return newDb; }