diff --git a/docs/android_sqlite/android_paging.md b/docs/android_sqlite/android_paging.md index aa784f0ce46..c6f4f7527b3 100644 --- a/docs/android_sqlite/android_paging.md +++ b/docs/android_sqlite/android_paging.md @@ -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 @@ -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. diff --git a/extensions/android-paging3/src/main/java/app/cash/sqldelight/paging3/KeyedQueryPagingSource.kt b/extensions/android-paging3/src/main/java/app/cash/sqldelight/paging3/KeyedQueryPagingSource.kt index 84cdc19ae64..a762ec1fc2a 100644 --- a/extensions/android-paging3/src/main/java/app/cash/sqldelight/paging3/KeyedQueryPagingSource.kt +++ b/extensions/android-paging3/src/main/java/app/cash/sqldelight/paging3/KeyedQueryPagingSource.kt @@ -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( private val queryProvider: (beginInclusive: Key, endExclusive: Key?) -> Query, private val pageBoundariesProvider: (anchor: Key?, limit: Long) -> Query, private val transacter: Transacter, - private val dispatcher: CoroutineDispatcher, + private val context: CoroutineContext, ) : QueryPagingSource() { private var pageBoundaries: List? = null @@ -27,7 +27,7 @@ internal class KeyedQueryPagingSource( } override suspend fun load(params: LoadParams): LoadResult { - return withContext(dispatcher) { + return withContext(context) { try { transacter.transactionWithResult { val boundaries = pageBoundaries diff --git a/extensions/android-paging3/src/main/java/app/cash/sqldelight/paging3/OffsetQueryPagingSource.kt b/extensions/android-paging3/src/main/java/app/cash/sqldelight/paging3/OffsetQueryPagingSource.kt index 2b8d72233e4..5d0aafb4dee 100644 --- a/extensions/android-paging3/src/main/java/app/cash/sqldelight/paging3/OffsetQueryPagingSource.kt +++ b/extensions/android-paging3/src/main/java/app/cash/sqldelight/paging3/OffsetQueryPagingSource.kt @@ -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( private val queryProvider: (limit: Long, offset: Long) -> Query, private val countQuery: Query, private val transacter: Transacter, - private val dispatcher: CoroutineDispatcher, + private val context: CoroutineContext, ) : QueryPagingSource() { override val jumpingSupported get() = true override suspend fun load( params: LoadParams, - ): LoadResult = withContext(dispatcher) { + ): LoadResult = withContext(context) { try { val key = params.key ?: 0L transacter.transactionWithResult { diff --git a/extensions/android-paging3/src/main/java/app/cash/sqldelight/paging3/QueryPagingSource.kt b/extensions/android-paging3/src/main/java/app/cash/sqldelight/paging3/QueryPagingSource.kt index b651c7be7de..045cfd11216 100644 --- a/extensions/android-paging3/src/main/java/app/cash/sqldelight/paging3/QueryPagingSource.kt +++ b/extensions/android-paging3/src/main/java/app/cash/sqldelight/paging3/QueryPagingSource.kt @@ -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 : @@ -55,19 +55,19 @@ internal abstract class QueryPagingSource : * OFFSET 100; * ``` * - * Queries will be executed on [dispatcher]. + * Queries will be executed on [context]. */ @Suppress("FunctionName") fun QueryPagingSource( countQuery: Query, transacter: Transacter, - dispatcher: CoroutineDispatcher = Dispatchers.IO, + context: CoroutineContext = Dispatchers.IO, queryProvider: (limit: Long, offset: Long) -> Query, ): PagingSource = OffsetQueryPagingSource( queryProvider, countQuery, transacter, - dispatcher, + context, ) /** @@ -125,7 +125,7 @@ fun 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. @@ -133,12 +133,12 @@ fun QueryPagingSource( @Suppress("FunctionName") fun QueryPagingSource( transacter: Transacter, - dispatcher: CoroutineDispatcher, + context: CoroutineContext, pageBoundariesProvider: (anchor: Key?, limit: Long) -> Query, queryProvider: (beginInclusive: Key, endExclusive: Key?) -> Query, ): PagingSource = KeyedQueryPagingSource( queryProvider, pageBoundariesProvider, transacter, - dispatcher, + context, ) diff --git a/extensions/android-paging3/src/test/java/app/cash/sqldelight/paging3/KeyedQueryPagingSourceTest.kt b/extensions/android-paging3/src/test/java/app/cash/sqldelight/paging3/KeyedQueryPagingSourceTest.kt index 1dc927645f5..5a8c9e51191 100644 --- a/extensions/android-paging3/src/test/java/app/cash/sqldelight/paging3/KeyedQueryPagingSourceTest.kt +++ b/extensions/android-paging3/src/test/java/app/cash/sqldelight/paging3/KeyedQueryPagingSourceTest.kt @@ -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 @@ -36,7 +36,7 @@ class KeyedQueryPagingSourceTest { queryProvider = this::query, pageBoundariesProvider = this::pageBoundaries, transacter = transacter, - dispatcher = TestCoroutineDispatcher(), + context = EmptyCoroutineContext, ) runBlocking { @@ -55,7 +55,7 @@ class KeyedQueryPagingSourceTest { queryProvider = this::query, pageBoundariesProvider = this::pageBoundaries, transacter = transacter, - dispatcher = TestCoroutineDispatcher(), + context = EmptyCoroutineContext, ) runBlocking { @@ -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)) } @@ -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)) } @@ -102,7 +102,7 @@ class KeyedQueryPagingSourceTest { queryProvider = this::query, pageBoundariesProvider = this::pageBoundaries, transacter = transacter, - dispatcher = TestCoroutineDispatcher(), + context = EmptyCoroutineContext, ) assertNull( @@ -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)) } @@ -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)) } diff --git a/extensions/android-paging3/src/test/java/app/cash/sqldelight/paging3/OffsetQueryPagingSourceTest.kt b/extensions/android-paging3/src/test/java/app/cash/sqldelight/paging3/OffsetQueryPagingSourceTest.kt index f5c11f1f70c..b63b74fdfe5 100644 --- a/extensions/android-paging3/src/test/java/app/cash/sqldelight/paging3/OffsetQueryPagingSourceTest.kt +++ b/extensions/android-paging3/src/test/java/app/cash/sqldelight/paging3/OffsetQueryPagingSourceTest.kt @@ -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 @@ -52,7 +52,7 @@ class OffsetQueryPagingSourceTest { this::query, countQuery(), transacter, - TestCoroutineDispatcher(), + EmptyCoroutineContext, ) val results = runBlocking { source.load(Refresh(null, 2, false)) } @@ -66,7 +66,7 @@ class OffsetQueryPagingSourceTest { this::query, countQuery(), transacter, - TestCoroutineDispatcher(), + EmptyCoroutineContext, ) val results = runBlocking { source.load(Refresh(null, 2, false)) } @@ -80,7 +80,7 @@ class OffsetQueryPagingSourceTest { this::query, countQuery(), transacter, - TestCoroutineDispatcher(), + EmptyCoroutineContext, ) val results = runBlocking { source.load(Refresh(8, 2, false)) } @@ -94,7 +94,7 @@ class OffsetQueryPagingSourceTest { this::query, countQuery(), transacter, - TestCoroutineDispatcher(), + EmptyCoroutineContext, ) runBlocking { @@ -113,7 +113,7 @@ class OffsetQueryPagingSourceTest { this::query, countQuery(), transacter, - TestCoroutineDispatcher(), + EmptyCoroutineContext, ) val results = runBlocking { source.load(Refresh(9, 2, false)) } @@ -127,7 +127,7 @@ class OffsetQueryPagingSourceTest { this::query, countQuery(), transacter, - TestCoroutineDispatcher(), + EmptyCoroutineContext, ) val results = runBlocking { source.load(Refresh(1L, 2, false)) } @@ -141,7 +141,7 @@ class OffsetQueryPagingSourceTest { this::query, countQuery(), transacter, - TestCoroutineDispatcher(), + EmptyCoroutineContext, ) val results = runBlocking { source.load(Refresh(null, 2, false)) } @@ -155,7 +155,7 @@ class OffsetQueryPagingSourceTest { this::query, countQuery(), transacter, - TestCoroutineDispatcher(), + EmptyCoroutineContext, ) val results = runBlocking { source.load(Refresh(4, 2, false)) } @@ -169,7 +169,7 @@ class OffsetQueryPagingSourceTest { this::query, countQuery(), transacter, - TestCoroutineDispatcher(), + EmptyCoroutineContext, ) val results = runBlocking { source.load(Refresh(8, 2, false)) } @@ -183,7 +183,7 @@ class OffsetQueryPagingSourceTest { this::query, countQuery(), transacter, - TestCoroutineDispatcher(), + EmptyCoroutineContext, ) val results = runBlocking { source.load(Refresh(9, 2, false)) } @@ -197,7 +197,7 @@ class OffsetQueryPagingSourceTest { this::query, countQuery(), transacter, - TestCoroutineDispatcher(), + EmptyCoroutineContext, ) val results = runBlocking { source.load(Refresh(1, 2, false)) } @@ -211,7 +211,7 @@ class OffsetQueryPagingSourceTest { this::query, countQuery(), transacter, - TestCoroutineDispatcher(), + EmptyCoroutineContext, ) runBlocking { @@ -230,7 +230,7 @@ class OffsetQueryPagingSourceTest { this::query, countQuery(), transacter, - TestCoroutineDispatcher(), + EmptyCoroutineContext, ) runBlocking { @@ -246,7 +246,7 @@ class OffsetQueryPagingSourceTest { { _, _ -> query }, countQuery(), transacter, - TestCoroutineDispatcher(), + EmptyCoroutineContext, ) runBlocking { source.load(Refresh(null, 0, false)) }