Using Bill Pugh Singleton Implementation
This commit is contained in:
parent
cfaccef2b3
commit
e7ce5c669e
@ -308,26 +308,22 @@ public class PodDBAdapter {
|
|||||||
KEY_CONTENT_ENCODED, KEY_FEED};
|
KEY_CONTENT_ENCODED, KEY_FEED};
|
||||||
|
|
||||||
private static Context context;
|
private static Context context;
|
||||||
private static PodDBHelper dbHelper;
|
|
||||||
|
|
||||||
private static volatile SQLiteDatabase db;
|
private static volatile SQLiteDatabase db;
|
||||||
private static int counter = 0;
|
private static int counter = 0;
|
||||||
private static PodDBAdapter instance;
|
|
||||||
|
|
||||||
public static void init(Context context) {
|
public static void init(Context context) {
|
||||||
PodDBAdapter.context = context.getApplicationContext();
|
PodDBAdapter.context = context.getApplicationContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class PodDBHelperholder {
|
// Bill Pugh Singleton Implementation
|
||||||
public static final PodDBHelper dbHelper = new PodDBHelper(PodDBAdapter.context, DATABASE_NAME, null);
|
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() {
|
public static PodDBAdapter getInstance() {
|
||||||
if (instance == null) {
|
return SingletonHolder.dbAdapter;
|
||||||
instance = new PodDBAdapter();
|
|
||||||
}
|
|
||||||
dbHelper = PodDBHelperholder.dbHelper;
|
|
||||||
return instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private PodDBAdapter() {
|
private PodDBAdapter() {
|
||||||
@ -346,11 +342,11 @@ public class PodDBAdapter {
|
|||||||
private SQLiteDatabase openDb() {
|
private SQLiteDatabase openDb() {
|
||||||
SQLiteDatabase newDb;
|
SQLiteDatabase newDb;
|
||||||
try {
|
try {
|
||||||
newDb = dbHelper.getWritableDatabase();
|
newDb = SingletonHolder.dbHelper.getWritableDatabase();
|
||||||
newDb.enableWriteAheadLogging();
|
newDb.enableWriteAheadLogging();
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
Log.e(TAG, Log.getStackTraceString(ex));
|
Log.e(TAG, Log.getStackTraceString(ex));
|
||||||
newDb = dbHelper.getReadableDatabase();
|
newDb = SingletonHolder.dbHelper.getReadableDatabase();
|
||||||
}
|
}
|
||||||
return newDb;
|
return newDb;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user