cashapp / sqldelight

SQLDelight - Generates typesafe Kotlin APIs from SQL
https://cashapp.github.io/sqldelight/
Apache License 2.0
6.05k stars 502 forks source link

Shrinking a query observed by OffsetQueryPagingSource in Android paging3 causes an exception #2591

Closed sproctor closed 1 year ago

sproctor commented 2 years ago

SQLDelight Version

1.5.1

Application Operating System

Android

Describe the Bug

The SQLDelight paging3 extension does not properly handle refresh.

https://github.com/cashapp/sqldelight/blob/de0ad1af76a6cc795e2c30ac0c76c635ee049d94/extensions/android-paging3/src/main/java/com/squareup/sqldelight/android/paging3/OffsetQueryPagingSource.kt#L40

This line could be something like:

val offset = if (key >= count) {
        maxOf(0, count - params.loadSize)
    } else {
        key
    }

Room has a good example of implementing paging: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:room/room-paging/src/main/kotlin/androidx/room/paging/LimitOffsetPagingSource.kt

Stacktrace

java.lang.IndexOutOfBoundsException
        at com.squareup.sqldelight.android.paging3.OffsetQueryPagingSource$load$2$1.invoke(OffsetQueryPagingSource.kt:40)
        at com.squareup.sqldelight.android.paging3.OffsetQueryPagingSource$load$2$1.invoke(OffsetQueryPagingSource.kt:38)
        at com.squareup.sqldelight.TransacterImpl.transactionWithWrapper(Transacter.kt:235)
        at com.squareup.sqldelight.TransacterImpl.transactionWithResult(Transacter.kt:221)
        at com.squareup.sqldelight.Transacter$DefaultImpls.transactionWithResult$default(Transacter.kt:61)
        at com.squareup.sqldelight.android.paging3.OffsetQueryPagingSource$load$2.invokeSuspend(OffsetQueryPagingSource.kt:38)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
ConorGarry commented 2 years ago

Having same issue in 1.5.2.

I have removed any potential deletions to ensure that I'm not concurrently altering the underlying database data during updates.

veyndan commented 1 year ago

This should be resolved by https://github.com/cashapp/sqldelight/pull/3396 which is in 2.0.0-alpha04.