Fix gc database during sync

This commit is contained in:
Koitharu
2023-04-17 09:56:17 +03:00
parent 1999f6f1a1
commit 5896d7abe7
2 changed files with 21 additions and 16 deletions

View File

@@ -142,8 +142,8 @@ class FavouritesRepository @Inject constructor(
suspend fun removeCategories(ids: Collection<Long>) { suspend fun removeCategories(ids: Collection<Long>) {
db.withTransaction { db.withTransaction {
for (id in ids) { for (id in ids) {
db.favouriteCategoriesDao.delete(id)
db.favouritesDao.deleteAll(id) db.favouritesDao.deleteAll(id)
db.favouriteCategoriesDao.delete(id)
} }
} }
// run after transaction success // run after transaction success

View File

@@ -65,14 +65,17 @@ class SyncHelper(
.url("$baseUrl/resource/$TABLE_FAVOURITES") .url("$baseUrl/resource/$TABLE_FAVOURITES")
.post(data.toRequestBody()) .post(data.toRequestBody())
.build() .build()
val response = httpClient.newCall(request).execute().parseJsonOrNull() ?: return val response = httpClient.newCall(request).execute().parseJsonOrNull()
val timestamp = response.getLong(FIELD_TIMESTAMP) if (response != null) {
val categoriesResult = upsertFavouriteCategories(response.getJSONArray(TABLE_FAVOURITE_CATEGORIES), timestamp) val timestamp = response.getLong(FIELD_TIMESTAMP)
syncResult.stats.numDeletes += categoriesResult.first().count?.toLong() ?: 0L val categoriesResult =
syncResult.stats.numInserts += categoriesResult.drop(1).sumOf { it.count?.toLong() ?: 0L } upsertFavouriteCategories(response.getJSONArray(TABLE_FAVOURITE_CATEGORIES), timestamp)
val favouritesResult = upsertFavourites(response.getJSONArray(TABLE_FAVOURITES), timestamp) syncResult.stats.numDeletes += categoriesResult.first().count?.toLong() ?: 0L
syncResult.stats.numDeletes += favouritesResult.first().count?.toLong() ?: 0L syncResult.stats.numInserts += categoriesResult.drop(1).sumOf { it.count?.toLong() ?: 0L }
syncResult.stats.numInserts += favouritesResult.drop(1).sumOf { it.count?.toLong() ?: 0L } val favouritesResult = upsertFavourites(response.getJSONArray(TABLE_FAVOURITES), timestamp)
syncResult.stats.numDeletes += favouritesResult.first().count?.toLong() ?: 0L
syncResult.stats.numInserts += favouritesResult.drop(1).sumOf { it.count?.toLong() ?: 0L }
}
gcFavourites() gcFavourites()
} }
@@ -84,13 +87,15 @@ class SyncHelper(
.url("$baseUrl/resource/$TABLE_HISTORY") .url("$baseUrl/resource/$TABLE_HISTORY")
.post(data.toRequestBody()) .post(data.toRequestBody())
.build() .build()
val response = httpClient.newCall(request).execute().parseJsonOrNull() ?: return val response = httpClient.newCall(request).execute().parseJsonOrNull()
val result = upsertHistory( if (response != null) {
json = response.getJSONArray(TABLE_HISTORY), val result = upsertHistory(
timestamp = response.getLong(FIELD_TIMESTAMP), json = response.getJSONArray(TABLE_HISTORY),
) timestamp = response.getLong(FIELD_TIMESTAMP),
syncResult.stats.numDeletes += result.first().count?.toLong() ?: 0L )
syncResult.stats.numInserts += result.drop(1).sumOf { it.count?.toLong() ?: 0L } syncResult.stats.numDeletes += result.first().count?.toLong() ?: 0L
syncResult.stats.numInserts += result.drop(1).sumOf { it.count?.toLong() ?: 0L }
}
gcHistory() gcHistory()
} }