Closed adam-hurwitz closed 4 years ago
When implementing PagingData
and PagingSource
with Kotlin Coroutines and making a Retrofit network request, as in the sample code above, Retrofit handles the background threading by default and returns on the main thread.
See StackOverflow: Does Retrofit make network calls on main thread?
When using RxJava with Retrofit as the network source, the threading needs to be explicitly specified as shown in the Android documentation sample.
See Android documentation: Page from network and database > Implement a RemoteMediator
Kotlin Coroutines
When implementing
PagingData
andPagingSource
with Kotlin Coroutines and making a Retrofit network request, as in the sample code above, Retrofit handles the background threading by default and returns on the main thread.See StackOverflow: Does Retrofit make network calls on main thread?
RxJava
When using RxJava with Retrofit as the network source, the threading needs to be explicitly specified as shown in the Android documentation sample.
See Android documentation: Page from network and database > Implement a RemoteMediator
i use local database in paging source [ROOM] can i have a solution to run the room calls in the background
Is background threading managed automatically with
PagingData
as it is withPagedList
, and then returned on the main thread?From the logs below, it appears
PagingData
is not run on the backgroud thread in the Paging 3 library compared toPagedList
in Paging 2. I've created logs within the Codelab sample inside the GithubPagingSource and SearchRepositoriesActivity classes, and re-created in my sample code below.Expect
override suspend fun load(...)
to run on an IO thread.viewModel.searchRepo(query).collectLatest { ... }
to run on the main thread.Observe
override suspend fun load(...)
and SearchRepositoriesActivityviewModel.searchRepo(query).collectLatest { ... }
run on the main thread.Paging 2
Threading is handled on the background by
PagedList
withtoLiveData
according to the documentation.Paging 3
The Paging 3 documentation does not mention how threading is managed. However, from the logs,
PagingSource
appears to be running the network request on the main thread and returning thePagingData
on the main thread.Sample
I've recreated the Codelab pattern in the CryptoTweets sample app app-simple module.
FeedPagingSource.kt
FeedRepository.kt
FeedViewModel.kt
Attempted Solution
In order to run the
PagingSource
on a background thread, the flow is initiated onDispatchers.IO
. However, the log still showsPagingSource
runs on the main thread in FeedPagingSource.kt.FeedViewModel.kt