Using Bill Pugh Singleton Implementation

This commit is contained in:
ByteHamster 2018-04-07 16:21:57 +02:00
parent cfaccef2b3
commit e7ce5c669e
1 changed files with 7 additions and 11 deletions

View File

@ -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;
}