Skip to content

Commit

Permalink
Reduce batch size when marking articles read (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
jocmp authored Jul 10, 2024
1 parent 3b39404 commit 9387690
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions app/src/main/java/com/capyreader/app/sync/Sync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,23 @@ fun removeStarAsync(articleID: String, context: Context) {
* Batch enqueue IDs to stay under 10KB work manager limit
* https://developer.android.com/jetpack/androidx/releases/work#1.0.0-alpha08
*/
fun markReadAsync(articleIDs: List<String>, context: Context) {
articleIDs.chunked(500) { batch ->
val data = Data
.Builder()
.putStringArray(ReadSyncWorker.ARTICLES_KEY, batch.toTypedArray())
.putBoolean(ReadSyncWorker.MARK_READ_KEY, true)
.build()

queueReadWorker(batch, data, context)
fun markReadAsync(articleIDs: List<String>, context: Context, batchSize: Int = 100) {
if (batchSize < 1) {
return
}

articleIDs.chunked(batchSize) { batch ->
try {
val data = Data
.Builder()
.putStringArray(ReadSyncWorker.ARTICLES_KEY, batch.toTypedArray())
.putBoolean(ReadSyncWorker.MARK_READ_KEY, true)
.build()

queueReadWorker(batch, data, context)
} catch (e: IllegalStateException) {
markReadAsync(context = context, articleIDs = batch, batchSize = batchSize / 2)
}
}
}

Expand Down

0 comments on commit 9387690

Please sign in to comment.