Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing in CoroutineContext to *QueryPagingSource #3384

Merged
merged 1 commit into from Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/android_sqlite/android_paging.md
Expand Up @@ -32,12 +32,12 @@ import com.squareup.sqldelight.android.paging3.QueryPagingSource
val pagingSource: PagingSource = QueryPagingSource(
countQuery = playerQueries.countPlayers(),
transacter = playerQueries,
dispatcher = Dispatchers.IO,
context = Dispatchers.IO,
queryProvider = playerQueries::players,
)
```

By default, queries are performed on `Dispatchers.IO` if no dispatcher is specified. Consumers expecting to use RxJava's `Scheduler` to perform queries should use the [`Scheduler.asCoroutineDispatcher`](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx2/kotlinx.coroutines.rx2/io.reactivex.-scheduler/as-coroutine-dispatcher.html) extension function.
By default, queries are performed on `Dispatchers.IO` if no context is specified. Consumers expecting to use RxJava's `Scheduler` to perform queries should use the [`Scheduler.asCoroutineDispatcher`](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx2/kotlinx.coroutines.rx2/io.reactivex.-scheduler/as-coroutine-dispatcher.html) extension function.

### Keyset Paging

Expand Down Expand Up @@ -86,10 +86,10 @@ import com.squareup.sqldelight.android.paging3.QueryPagingSource

val keyedSource = QueryPagingSource(
transacter = playerQueries,
dispatcher = Dispatchers.IO,
context = Dispatchers.IO,
pageBoundariesProvider = playerQueries::pageBoundaries,
queryProvider = playerQueries::keyedQuery,
)
```

By default, queries are performed on `Dispatchers.IO` if no dispatcher is specified. Consumers expecting to use RxJava's `Scheduler` to perform queries should use the [`Scheduler.asCoroutineDispatcher`](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx2/kotlinx.coroutines.rx2/io.reactivex.-scheduler/as-coroutine-dispatcher.html) extension function.
By default, queries are performed on `Dispatchers.IO` if no context is specified. Consumers expecting to use RxJava's `Scheduler` to perform queries should use the [`Scheduler.asCoroutineDispatcher`](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx2/kotlinx.coroutines.rx2/io.reactivex.-scheduler/as-coroutine-dispatcher.html) extension function.
Expand Up @@ -3,14 +3,14 @@ package app.cash.sqldelight.paging3
import androidx.paging.PagingState
import app.cash.sqldelight.Query
import app.cash.sqldelight.Transacter
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext

internal class KeyedQueryPagingSource<Key : Any, RowType : Any>(
private val queryProvider: (beginInclusive: Key, endExclusive: Key?) -> Query<RowType>,
private val pageBoundariesProvider: (anchor: Key?, limit: Long) -> Query<Key>,
private val transacter: Transacter,
private val dispatcher: CoroutineDispatcher,
private val context: CoroutineContext,
) : QueryPagingSource<Key, RowType>() {

private var pageBoundaries: List<Key>? = null
Expand All @@ -27,7 +27,7 @@ internal class KeyedQueryPagingSource<Key : Any, RowType : Any>(
}

override suspend fun load(params: LoadParams<Key>): LoadResult<Key, RowType> {
return withContext(dispatcher) {
return withContext(context) {
try {
transacter.transactionWithResult {
val boundaries = pageBoundaries
Expand Down
Expand Up @@ -18,21 +18,21 @@ package app.cash.sqldelight.paging3
import androidx.paging.PagingState
import app.cash.sqldelight.Query
import app.cash.sqldelight.Transacter
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext

internal class OffsetQueryPagingSource<RowType : Any>(
private val queryProvider: (limit: Long, offset: Long) -> Query<RowType>,
private val countQuery: Query<Long>,
private val transacter: Transacter,
private val dispatcher: CoroutineDispatcher,
private val context: CoroutineContext,
) : QueryPagingSource<Long, RowType>() {

override val jumpingSupported get() = true

override suspend fun load(
params: LoadParams<Long>,
): LoadResult<Long, RowType> = withContext(dispatcher) {
): LoadResult<Long, RowType> = withContext(context) {
try {
val key = params.key ?: 0L
transacter.transactionWithResult {
Expand Down
Expand Up @@ -19,8 +19,8 @@ import androidx.paging.PagingConfig
import androidx.paging.PagingSource
import app.cash.sqldelight.Query
import app.cash.sqldelight.Transacter
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.CoroutineContext
import kotlin.properties.Delegates

internal abstract class QueryPagingSource<Key : Any, RowType : Any> :
Expand Down Expand Up @@ -55,19 +55,19 @@ internal abstract class QueryPagingSource<Key : Any, RowType : Any> :
* OFFSET 100;
* ```
*
* Queries will be executed on [dispatcher].
* Queries will be executed on [context].
*/
@Suppress("FunctionName")
fun <RowType : Any> QueryPagingSource(
countQuery: Query<Long>,
transacter: Transacter,
dispatcher: CoroutineDispatcher = Dispatchers.IO,
context: CoroutineContext = Dispatchers.IO,
queryProvider: (limit: Long, offset: Long) -> Query<RowType>,
): PagingSource<Long, RowType> = OffsetQueryPagingSource(
queryProvider,
countQuery,
transacter,
dispatcher,
context,
)

/**
Expand Down Expand Up @@ -125,20 +125,20 @@ fun <RowType : Any> QueryPagingSource(
* ORDER BY value ASC;
* ```
*
* Queries will be executed on [dispatcher].
* Queries will be executed on [context].
*
* This [PagingSource] _does not_ support jumping. If your use case requires jumping, use the
* offset based [QueryPagingSource] function.
*/
@Suppress("FunctionName")
fun <Key : Any, RowType : Any> QueryPagingSource(
transacter: Transacter,
dispatcher: CoroutineDispatcher,
context: CoroutineContext,
pageBoundariesProvider: (anchor: Key?, limit: Long) -> Query<Key>,
queryProvider: (beginInclusive: Key, endExclusive: Key?) -> Query<RowType>,
): PagingSource<Key, RowType> = KeyedQueryPagingSource(
queryProvider,
pageBoundariesProvider,
transacter,
dispatcher,
context,
)
Expand Up @@ -12,9 +12,9 @@ import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestCoroutineDispatcher
import org.junit.Before
import org.junit.Test
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.test.assertEquals
import kotlin.test.assertNull

Expand All @@ -36,7 +36,7 @@ class KeyedQueryPagingSourceTest {
queryProvider = this::query,
pageBoundariesProvider = this::pageBoundaries,
transacter = transacter,
dispatcher = TestCoroutineDispatcher(),
context = EmptyCoroutineContext,
)

runBlocking {
Expand All @@ -55,7 +55,7 @@ class KeyedQueryPagingSourceTest {
queryProvider = this::query,
pageBoundariesProvider = this::pageBoundaries,
transacter = transacter,
dispatcher = TestCoroutineDispatcher(),
context = EmptyCoroutineContext,
)

runBlocking {
Expand All @@ -74,7 +74,7 @@ class KeyedQueryPagingSourceTest {
queryProvider = this::query,
pageBoundariesProvider = this::pageBoundaries,
transacter = transacter,
dispatcher = TestCoroutineDispatcher(),
context = EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(key = 5L, loadSize = 2, false)) }
Expand All @@ -87,7 +87,7 @@ class KeyedQueryPagingSourceTest {
queryProvider = this::query,
pageBoundariesProvider = this::pageBoundaries,
transacter = transacter,
dispatcher = TestCoroutineDispatcher(),
context = EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(key = 9L, loadSize = 3, false)) }
Expand All @@ -102,7 +102,7 @@ class KeyedQueryPagingSourceTest {
queryProvider = this::query,
pageBoundariesProvider = this::pageBoundaries,
transacter = transacter,
dispatcher = TestCoroutineDispatcher(),
context = EmptyCoroutineContext,
)

assertNull(
Expand All @@ -122,7 +122,7 @@ class KeyedQueryPagingSourceTest {
queryProvider = this::query,
pageBoundariesProvider = this::pageBoundaries,
transacter = transacter,
dispatcher = TestCoroutineDispatcher(),
context = EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(key = null, loadSize = 3, false)) }
Expand All @@ -143,7 +143,7 @@ class KeyedQueryPagingSourceTest {
queryProvider = this::query,
pageBoundariesProvider = this::pageBoundaries,
transacter = transacter,
dispatcher = TestCoroutineDispatcher(),
context = EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(key = 6L, loadSize = 3, false)) }
Expand Down
Expand Up @@ -25,12 +25,12 @@ import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestCoroutineDispatcher
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.test.assertFailsWith

@ExperimentalCoroutinesApi
Expand All @@ -52,7 +52,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(null, 2, false)) }
Expand All @@ -66,7 +66,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(null, 2, false)) }
Expand All @@ -80,7 +80,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(8, 2, false)) }
Expand All @@ -94,7 +94,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

runBlocking {
Expand All @@ -113,7 +113,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(9, 2, false)) }
Expand All @@ -127,7 +127,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(1L, 2, false)) }
Expand All @@ -141,7 +141,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(null, 2, false)) }
Expand All @@ -155,7 +155,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(4, 2, false)) }
Expand All @@ -169,7 +169,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(8, 2, false)) }
Expand All @@ -183,7 +183,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(9, 2, false)) }
Expand All @@ -197,7 +197,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(1, 2, false)) }
Expand All @@ -211,7 +211,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

runBlocking {
Expand All @@ -230,7 +230,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

runBlocking {
Expand All @@ -246,7 +246,7 @@ class OffsetQueryPagingSourceTest {
{ _, _ -> query },
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

runBlocking { source.load(Refresh(null, 0, false)) }
Expand Down