Fix threading issue in RecipientDatabase.
parent
acea24c19c
commit
5f993ed0f7
|
@ -188,18 +188,24 @@ public class RecipientDatabase extends Database {
|
|||
String query = PHONE + " = ?";
|
||||
String[] args = new String[] { e164 };
|
||||
|
||||
synchronized (this) {
|
||||
try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID)));
|
||||
} else {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(PHONE, e164);
|
||||
long id = db.insertOrThrow(TABLE_NAME, null, values);
|
||||
if (id < 0) throw new AssertionError("Failed to insert recipient!");
|
||||
return RecipientId.from(id);
|
||||
}
|
||||
db.beginTransaction();
|
||||
|
||||
try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
db.setTransactionSuccessful();
|
||||
return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID)));
|
||||
} else {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(PHONE, e164);
|
||||
|
||||
long id = db.insertOrThrow(TABLE_NAME, null, values);
|
||||
if (id < 0) throw new AssertionError("Failed to insert recipient!");
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
return RecipientId.from(id);
|
||||
}
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -212,18 +218,24 @@ public class RecipientDatabase extends Database {
|
|||
String query = EMAIL + " = ?";
|
||||
String[] args = new String[] { email };
|
||||
|
||||
synchronized (this) {
|
||||
try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID)));
|
||||
} else {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(EMAIL, email);
|
||||
long id = db.insertOrThrow(TABLE_NAME, null, values);
|
||||
if (id < 0) throw new AssertionError("Failed to insert recipient!");
|
||||
return RecipientId.from(id);
|
||||
}
|
||||
db.beginTransaction();
|
||||
|
||||
try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
db.setTransactionSuccessful();
|
||||
return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID)));
|
||||
} else {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(EMAIL, email);
|
||||
|
||||
long id = db.insertOrThrow(TABLE_NAME, null, values);
|
||||
if (id < 0) throw new AssertionError("Failed to insert recipient!");
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
return RecipientId.from(id);
|
||||
}
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -236,18 +248,24 @@ public class RecipientDatabase extends Database {
|
|||
String query = GROUP_ID + " = ?";
|
||||
String[] args = new String[] { groupId };
|
||||
|
||||
synchronized (this) {
|
||||
try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID)));
|
||||
} else {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(GROUP_ID, groupId);
|
||||
long id = db.insertOrThrow(TABLE_NAME, null, values);
|
||||
if (id < 0) throw new AssertionError("Failed to insert recipient!");
|
||||
return RecipientId.from(id);
|
||||
}
|
||||
db.beginTransaction();
|
||||
|
||||
try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
db.setTransactionSuccessful();
|
||||
return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID)));
|
||||
} else {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(GROUP_ID, groupId);
|
||||
|
||||
long id = db.insertOrThrow(TABLE_NAME, null, values);
|
||||
if (id < 0) throw new AssertionError("Failed to insert recipient!");
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
return RecipientId.from(id);
|
||||
}
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue