mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Never store reference to SQLiteDatabase
SQLiteDatabase closes automatically at random times. Storing a reference to an open SQLiteDatabase eventually leads to a crash. See: https://stackoverflow.com/questions/1483629/
This commit is contained in:
@@ -53,38 +53,27 @@ public class SQLiteCheckmarkList extends CheckmarkList
|
||||
@Nullable
|
||||
private CachedData cache;
|
||||
|
||||
@NonNull
|
||||
private final SQLiteStatement invalidateStatement;
|
||||
|
||||
@NonNull
|
||||
private final SQLiteStatement addStatement;
|
||||
|
||||
@NonNull
|
||||
private final SQLiteDatabase db;
|
||||
|
||||
public SQLiteCheckmarkList(Habit habit)
|
||||
{
|
||||
super(habit);
|
||||
sqlite = new SQLiteUtils<>(CheckmarkRecord.class);
|
||||
|
||||
db = Cache.openDatabase();
|
||||
addStatement = db.compileStatement(ADD_QUERY);
|
||||
invalidateStatement = db.compileStatement(INVALIDATE_QUERY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(List<Checkmark> checkmarks)
|
||||
{
|
||||
check(habit.getId());
|
||||
SQLiteDatabase db = Cache.openDatabase();
|
||||
SQLiteStatement statement = db.compileStatement(ADD_QUERY);
|
||||
db.beginTransaction();
|
||||
try
|
||||
{
|
||||
for (Checkmark c : checkmarks)
|
||||
{
|
||||
addStatement.bindLong(1, habit.getId());
|
||||
addStatement.bindLong(2, c.getTimestamp());
|
||||
addStatement.bindLong(3, c.getValue());
|
||||
addStatement.execute();
|
||||
statement.bindLong(1, habit.getId());
|
||||
statement.bindLong(2, c.getTimestamp());
|
||||
statement.bindLong(3, c.getValue());
|
||||
statement.execute();
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
@@ -132,9 +121,11 @@ public class SQLiteCheckmarkList extends CheckmarkList
|
||||
public void invalidateNewerThan(long timestamp)
|
||||
{
|
||||
cache = null;
|
||||
invalidateStatement.bindLong(1, habit.getId());
|
||||
invalidateStatement.bindLong(2, timestamp);
|
||||
invalidateStatement.execute();
|
||||
SQLiteDatabase db = Cache.openDatabase();
|
||||
SQLiteStatement statement = db.compileStatement(INVALIDATE_QUERY);
|
||||
statement.bindLong(1, habit.getId());
|
||||
statement.bindLong(2, timestamp);
|
||||
statement.execute();
|
||||
observable.notifyListeners();
|
||||
}
|
||||
|
||||
|
||||
@@ -49,14 +49,6 @@ public class SQLiteScoreList extends ScoreList
|
||||
@NonNull
|
||||
private final SQLiteUtils<ScoreRecord> sqlite;
|
||||
|
||||
@NonNull
|
||||
private final SQLiteStatement invalidateStatement;
|
||||
|
||||
@NonNull
|
||||
private final SQLiteStatement addStatement;
|
||||
|
||||
private final SQLiteDatabase db;
|
||||
|
||||
@Nullable
|
||||
private CachedData cache = null;
|
||||
|
||||
@@ -69,25 +61,23 @@ public class SQLiteScoreList extends ScoreList
|
||||
{
|
||||
super(habit);
|
||||
sqlite = new SQLiteUtils<>(ScoreRecord.class);
|
||||
|
||||
db = Cache.openDatabase();
|
||||
addStatement = db.compileStatement(ADD_QUERY);
|
||||
invalidateStatement = db.compileStatement(INVALIDATE_QUERY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(List<Score> scores)
|
||||
{
|
||||
check(habit.getId());
|
||||
SQLiteDatabase db = Cache.openDatabase();
|
||||
SQLiteStatement statement = db.compileStatement(ADD_QUERY);
|
||||
db.beginTransaction();
|
||||
try
|
||||
{
|
||||
for (Score s : scores)
|
||||
{
|
||||
addStatement.bindLong(1, habit.getId());
|
||||
addStatement.bindLong(2, s.getTimestamp());
|
||||
addStatement.bindLong(3, s.getValue());
|
||||
addStatement.execute();
|
||||
statement.bindLong(1, habit.getId());
|
||||
statement.bindLong(2, s.getTimestamp());
|
||||
statement.bindLong(3, s.getValue());
|
||||
statement.execute();
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
@@ -150,9 +140,11 @@ public class SQLiteScoreList extends ScoreList
|
||||
public synchronized void invalidateNewerThan(long timestamp)
|
||||
{
|
||||
cache = null;
|
||||
invalidateStatement.bindLong(1, habit.getId());
|
||||
invalidateStatement.bindLong(2, timestamp);
|
||||
invalidateStatement.execute();
|
||||
SQLiteDatabase db = Cache.openDatabase();
|
||||
SQLiteStatement statement = db.compileStatement(INVALIDATE_QUERY);
|
||||
statement.bindLong(1, habit.getId());
|
||||
statement.bindLong(2, timestamp);
|
||||
statement.execute();
|
||||
getObservable().notifyListeners();
|
||||
}
|
||||
|
||||
|
||||
@@ -37,22 +37,19 @@ import java.util.*;
|
||||
*/
|
||||
public class SQLiteStreakList extends StreakList
|
||||
{
|
||||
|
||||
private static final String INVALIDATE_QUERY =
|
||||
"delete from Streak where habit = ? and end >= ?";
|
||||
|
||||
private HabitRecord habitRecord;
|
||||
|
||||
@NonNull
|
||||
private final SQLiteUtils<StreakRecord> sqlite;
|
||||
|
||||
private final SQLiteStatement invalidateStatement;
|
||||
|
||||
public SQLiteStreakList(Habit habit)
|
||||
{
|
||||
super(habit);
|
||||
sqlite = new SQLiteUtils<>(StreakRecord.class);
|
||||
|
||||
SQLiteDatabase db = Cache.openDatabase();
|
||||
String invalidateQuery = "delete from Streak where habit = ? " +
|
||||
"and end >= ?";
|
||||
invalidateStatement = db.compileStatement(invalidateQuery);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -81,9 +78,11 @@ public class SQLiteStreakList extends StreakList
|
||||
@Override
|
||||
public void invalidateNewerThan(long timestamp)
|
||||
{
|
||||
invalidateStatement.bindLong(1, habit.getId());
|
||||
invalidateStatement.bindLong(2, timestamp - DateUtils.millisecondsInOneDay);
|
||||
invalidateStatement.execute();
|
||||
SQLiteDatabase db = Cache.openDatabase();
|
||||
SQLiteStatement statement = db.compileStatement(INVALIDATE_QUERY);
|
||||
statement.bindLong(1, habit.getId());
|
||||
statement.bindLong(2, timestamp - DateUtils.millisecondsInOneDay);
|
||||
statement.execute();
|
||||
observable.notifyListeners();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user