Skip to content

Commit

Permalink
Added async writes on all DB operations
Browse files Browse the repository at this point in the history
  • Loading branch information
thuryn committed Mar 21, 2021
1 parent 77672ae commit 881f5f7
Show file tree
Hide file tree
Showing 19 changed files with 552 additions and 440 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
v5.7.3
Fix update of forecast, several small fixies, added logging

v5.7.1
Updated DB versions

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ android {
applicationId "org.thosp.yourlocalweather"
minSdkVersion 14
targetSdkVersion 29
versionCode 141
versionName "5.7.2"
versionCode 142
versionName "5.7.3"
vectorDrawables.useSupportLibrary true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
playAccountConfig = playAccountConfigs.defaultAccountConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,22 @@ public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

private void createLicenseKey(LicenseKey licenseKey) {
SQLiteDatabase db = getWritableDatabase();

ContentValues values = new ContentValues();
values.put(LicenseKeysContract.LicenseKeys.COLUMN_NAME_INITIAL_LICENSE,
licenseKey.getInitialLicense());
values.put(LicenseKeysContract.LicenseKeys.COLUMN_NAME_REQUEST_URI,
licenseKey.getRequestUri());
values.put(LicenseKeysContract.LicenseKeys.COLUMN_NAME_TOKEN,
licenseKey.getToken());

long newLocationRowId = db.insert(LicenseKeysContract.LicenseKeys.TABLE_NAME, null, values);
appendLog(context, TAG, "LicenseKey created: ", newLocationRowId);
new Thread(new Runnable() {
public void run() {
SQLiteDatabase db = getWritableDatabase();

ContentValues values = new ContentValues();
values.put(LicenseKeysContract.LicenseKeys.COLUMN_NAME_INITIAL_LICENSE,
licenseKey.getInitialLicense());
values.put(LicenseKeysContract.LicenseKeys.COLUMN_NAME_REQUEST_URI,
licenseKey.getRequestUri());
values.put(LicenseKeysContract.LicenseKeys.COLUMN_NAME_TOKEN,
licenseKey.getToken());

long newLocationRowId = db.insert(LicenseKeysContract.LicenseKeys.TABLE_NAME, null, values);
appendLog(context, TAG, "LicenseKey created: ", newLocationRowId);
}
}).start();
}

public LicenseKey getLicenseKeyByLocationRequestId(String requestUri) {
Expand Down Expand Up @@ -107,21 +111,25 @@ public LicenseKey getLicenseKeyByLocationRequestId(String requestUri) {
}

public void updateToken(String requestUri, String token) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(LicenseKeysContract.LicenseKeys.COLUMN_NAME_TOKEN, token);
values.put(LicenseKeysContract.LicenseKeys.COLUMN_NAME_LAST_CALL_TIME_IN_MS, System.currentTimeMillis());
if (!dbRecordExists(requestUri)) {
values.put(LicenseKeysContract.LicenseKeys.COLUMN_NAME_REQUEST_URI, requestUri);
db.insert(LicenseKeysContract.LicenseKeys.TABLE_NAME, null, values);
} else {
db.updateWithOnConflict(
LicenseKeysContract.LicenseKeys.TABLE_NAME,
values,
LicenseKeysContract.LicenseKeys.COLUMN_NAME_REQUEST_URI + "='" + requestUri + "'",
null,
SQLiteDatabase.CONFLICT_IGNORE);
}
new Thread(new Runnable() {
public void run() {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(LicenseKeysContract.LicenseKeys.COLUMN_NAME_TOKEN, token);
values.put(LicenseKeysContract.LicenseKeys.COLUMN_NAME_LAST_CALL_TIME_IN_MS, System.currentTimeMillis());
if (!dbRecordExists(requestUri)) {
values.put(LicenseKeysContract.LicenseKeys.COLUMN_NAME_REQUEST_URI, requestUri);
db.insert(LicenseKeysContract.LicenseKeys.TABLE_NAME, null, values);
} else {
db.updateWithOnConflict(
LicenseKeysContract.LicenseKeys.TABLE_NAME,
values,
LicenseKeysContract.LicenseKeys.COLUMN_NAME_REQUEST_URI + "='" + requestUri + "'",
null,
SQLiteDatabase.CONFLICT_IGNORE);
}
}
}).start();
}

private boolean dbRecordExists(String requestUri) {
Expand Down
352 changes: 196 additions & 156 deletions app/src/main/java/org/thosp/yourlocalweather/model/LocationsDbHelper.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -214,47 +214,51 @@ public List<Location> getAllRows() {
}

public void deleteRecordFromTable(Location location) {
int deletedOrderId = location.getOrderId();
SQLiteDatabase db = getWritableDatabase();
String selection = LocationsContract.Locations._ID + " = ?";
String[] selectionArgs = {location.getId().toString()};
db.delete(LocationsContract.Locations.TABLE_NAME, selection, selectionArgs);

String[] projection = {
LocationsContract.Locations._ID,
LocationsContract.Locations.COLUMN_NAME_ORDER_ID
};

String sortOrder = LocationsContract.Locations.COLUMN_NAME_ORDER_ID;

Cursor cursor = null;
try {
cursor = db.query(
LocationsContract.Locations.TABLE_NAME,
projection,
LocationsContract.Locations.COLUMN_NAME_ORDER_ID + ">" + deletedOrderId,
null,
null,
null,
sortOrder
);

while (cursor.moveToNext()) {
long itemId = cursor.getInt(cursor.getColumnIndexOrThrow(LocationsContract.Locations._ID));
int orderId = cursor.getInt(cursor.getColumnIndexOrThrow(LocationsContract.Locations.COLUMN_NAME_ORDER_ID));
ContentValues values = new ContentValues();
values.put(LocationsContract.Locations.COLUMN_NAME_ORDER_ID, orderId - 1);
db.updateWithOnConflict(
LocationsContract.Locations.TABLE_NAME,
values,
LocationsContract.Locations._ID +"=" + itemId,
null,
SQLiteDatabase.CONFLICT_IGNORE);
}
} finally {
if (cursor != null) {
cursor.close();
new Thread(new Runnable() {
public void run() {
int deletedOrderId = location.getOrderId();
SQLiteDatabase db = getWritableDatabase();
String selection = LocationsContract.Locations._ID + " = ?";
String[] selectionArgs = {location.getId().toString()};
db.delete(LocationsContract.Locations.TABLE_NAME, selection, selectionArgs);

String[] projection = {
LocationsContract.Locations._ID,
LocationsContract.Locations.COLUMN_NAME_ORDER_ID
};

String sortOrder = LocationsContract.Locations.COLUMN_NAME_ORDER_ID;

Cursor cursor = null;
try {
cursor = db.query(
LocationsContract.Locations.TABLE_NAME,
projection,
LocationsContract.Locations.COLUMN_NAME_ORDER_ID + ">" + deletedOrderId,
null,
null,
null,
sortOrder
);

while (cursor.moveToNext()) {
long itemId = cursor.getInt(cursor.getColumnIndexOrThrow(LocationsContract.Locations._ID));
int orderId = cursor.getInt(cursor.getColumnIndexOrThrow(LocationsContract.Locations.COLUMN_NAME_ORDER_ID));
ContentValues values = new ContentValues();
values.put(LocationsContract.Locations.COLUMN_NAME_ORDER_ID, orderId - 1);
db.updateWithOnConflict(
LocationsContract.Locations.TABLE_NAME,
values,
LocationsContract.Locations._ID +"=" + itemId,
null,
SQLiteDatabase.CONFLICT_IGNORE);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
}).start();
}
}
Loading

0 comments on commit 881f5f7

Please sign in to comment.