mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-24 16:57:50 +00:00
maintain one ContactsDatabase instance per Loader
Fixes #3004 Closes #3041 Closes #3034 // FREEBIE
This commit is contained in:
parent
7b3bd2fbf7
commit
b3707021b1
@ -86,12 +86,6 @@ public class PushContactSelectionListFragment extends Fragment
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
ContactsDatabase.destroyInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.push_contact_selection_list_activity, container, false);
|
||||
|
@ -19,6 +19,11 @@ package org.thoughtcrime.securesms.contacts;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.support.v4.content.CursorLoader;
|
||||
import android.util.Log;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
/**
|
||||
* CursorLoader that initializes a ContactsDatabase instance
|
||||
@ -26,10 +31,13 @@ import android.support.v4.content.CursorLoader;
|
||||
* @author Jake McGinty
|
||||
*/
|
||||
public class ContactsCursorLoader extends CursorLoader {
|
||||
private static final String TAG = ContactsCursorLoader.class.getSimpleName();
|
||||
private static final int DB_PERMITS = 100;
|
||||
|
||||
private final Context context;
|
||||
private final String filter;
|
||||
private final boolean pushOnly;
|
||||
private final Semaphore dbSemaphore = new Semaphore(DB_PERMITS);
|
||||
private ContactsDatabase db;
|
||||
|
||||
public ContactsCursorLoader(Context context, String filter, boolean pushOnly) {
|
||||
@ -37,17 +45,33 @@ public class ContactsCursorLoader extends CursorLoader {
|
||||
this.context = context;
|
||||
this.filter = filter;
|
||||
this.pushOnly = pushOnly;
|
||||
this.db = new ContactsDatabase(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor loadInBackground() {
|
||||
ContactsDatabase.destroyInstance();
|
||||
db = ContactsDatabase.getInstance(context);
|
||||
return db.query(filter, pushOnly);
|
||||
try {
|
||||
dbSemaphore.acquire();
|
||||
return db.query(filter, pushOnly);
|
||||
} catch (InterruptedException ie) {
|
||||
throw new AssertionError(ie);
|
||||
} finally {
|
||||
dbSemaphore.release();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReset() {
|
||||
Log.w(TAG, "onReset()");
|
||||
try {
|
||||
dbSemaphore.acquire(DB_PERMITS);
|
||||
db.close();
|
||||
db = new ContactsDatabase(context);
|
||||
} catch (InterruptedException ie) {
|
||||
throw new AssertionError(ie);
|
||||
} finally {
|
||||
dbSemaphore.release(DB_PERMITS);
|
||||
}
|
||||
super.onReset();
|
||||
}
|
||||
}
|
||||
|
@ -75,19 +75,7 @@ public class ContactsDatabase {
|
||||
public static final int PUSH_TYPE = 1;
|
||||
public static final int GROUP_TYPE = 2;
|
||||
|
||||
private static ContactsDatabase instance = null;
|
||||
|
||||
public synchronized static ContactsDatabase getInstance(Context context) {
|
||||
if (instance == null) instance = new ContactsDatabase(context);
|
||||
return instance;
|
||||
}
|
||||
|
||||
public synchronized static void destroyInstance() {
|
||||
if (instance != null) instance.close();
|
||||
instance = null;
|
||||
}
|
||||
|
||||
private ContactsDatabase(Context context) {
|
||||
public ContactsDatabase(Context context) {
|
||||
this.dbHelper = new DatabaseOpenHelper(context);
|
||||
this.context = context;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user