synchronize saveSummary and getSummary. They can be called from

different threads and that may be the cause of a few crashes I've seen
and had reported.
This commit is contained in:
Andy2 2010-06-14 18:21:12 -07:00
parent ca2aa982a7
commit 391982f16f

View file

@ -38,10 +38,10 @@ public class DBUtils {
public static GameSummary getSummary( Context context, String file )
{
initDB( context );
GameSummary summary = new GameSummary();
SQLiteDatabase db = m_dbHelper.getReadableDatabase();
synchronized( m_dbHelper ) {
SQLiteDatabase db = m_dbHelper.getReadableDatabase();
String[] columns = { DBHelper.NUM_MOVES, DBHelper.GAME_OVER,
DBHelper.CONTYPE, DBHelper.ROOMNAME,
DBHelper.SMSPHONE, DBHelper.SCORES
@ -58,8 +58,8 @@ public class DBUtils {
getColumnIndex(DBHelper.GAME_OVER));
summary.gameOver = tmp == 0 ? false : true;
String scoresStr = cursor.getString( cursor.
getColumnIndex(DBHelper.SCORES));
String scoresStr =
cursor.getString( cursor.getColumnIndex(DBHelper.SCORES));
StringTokenizer st = new StringTokenizer( scoresStr );
int[] scores = new int[st.countTokens()];
for ( int ii = 0; ii < scores.length; ++ii ) {
@ -85,11 +85,13 @@ public class DBUtils {
}
cursor.close();
db.close();
}
return summary;
}
public static void saveSummary( String path, GameSummary summary )
{
synchronized( m_dbHelper ) {
SQLiteDatabase db = m_dbHelper.getWritableDatabase();
if ( null == summary ) {
@ -117,13 +119,15 @@ public class DBUtils {
Utils.logf( "saveSummary: nMoves=%d", summary.nMoves );
try {
long result = db.replaceOrThrow( DBHelper.TABLE_NAME, "", values );
long result = db.replaceOrThrow( DBHelper.TABLE_NAME, "",
values );
} catch ( Exception ex ) {
Utils.logf( "ex: %s", ex.toString() );
}
}
db.close();
}
}
private static void initDB( Context context )
{