I’m encountering slow loading as I scroll because of multiple network calls that need to be made.
class RepoResultPagingSource(
private val repository: Repository,
private val query: String
) : PagingSource<Int, Result>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Result> {
try {
val page = params.key ?: 1
// network call to get repositories
val repos = repository.getRepositories(query, page, PAGE_SIZE)
val results = repos.map { repo ->
// uses repo to get its top contributor
val contributor = repository.getTopContributor(repo.owner.username, repo.reponame)
Result(repo.owner.url, repo.reponame, contributor.username?: "")
}
return LoadResult.Page(
data = repoResults,
prevKey = if (page == 1) null else page-1,
nextKey = page + 1
)
} catch (e: Exception) {
return LoadResult.Error(e)
}
}
class Repository(private val api: RepositoryApi,
private val dispatcher: CoroutineDispatcher = Dispatchers.IO) {
suspend fun getRepositories(query: String, page: Int, perPage: Int): List<Repository>
{
return withContext(dispatcher) {
api.search(query, page, perPage).repositories
}
}
suspend fun getTopContributor(ownerName: String, repoName: String): Contributor {
return withContext(dispatcher) {
api.getContributors(ownerName, repoName, 1).first()
}
}
}
I realized that load was blocking because the paging library calls it from runBlockin
@JvmStatic
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public fun <K : Any, T : Any> create(
pagingSource: PagingSource<K, T>,
initialPage: PagingSource.LoadResult.Page<K, T>?,
coroutineScope: CoroutineScope,
notifyDispatcher: CoroutineDispatcher,
fetchDispatcher: CoroutineDispatcher,
boundaryCallback: BoundaryCallback<T>?,
config: Config,
key: K?
): PagedList<T> {
val resolvedInitialPage = when (initialPage) {
null -> {
// Compatibility codepath - perform the initial load immediately, since caller
// hasn't done it. We block in this case, but it's only used in the legacy path.
val params = PagingSource.LoadParams.Refresh(
key,
config.initialLoadSizeHint,
config.enablePlaceholders,
)
runBlocking {
val initialResult = pagingSource.load(params)
when (initialResult) {
is PagingSource.LoadResult.Page -> initialResult
is PagingSource.LoadResult.Error -> throw initialResult.throwable
is PagingSource.LoadResult.Invalid ->
throw IllegalStateException(
"Failed to create PagedList. The provided PagingSource " +
"returned LoadResult.Invalid, but a LoadResult.Page was " +
"expected. To use a PagingSource which supports " +
"invalidation, use a PagedList builder that accepts a " +
"factory method for PagingSource or DataSource.Factory, " +
"such as LivePagedList."
)
}
}
}
else -> initialPage
}
return ContiguousPagedList(
pagingSource,
coroutineScope,
notifyDispatcher,
fetchDispatcher,
boundaryCallback,
config,
resolvedInitialPage,
key
)
}
So I inject a viewModelScope to start a nonblocking coroutine like
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Result> {
try {
return scope.async {
val page = params.key ?: 1
// network call to get repositories
val repos = repository.getRepositories(query, page, 10)
val results = repos.map { repo ->
// uses repo to get its top contributor
val contributor = repository.getTopContributor(repo.owner.username,
repo.reponame)
Result(repo.owner.url, repo.reponame, contributor.username?: "")
}
return@async LoadResult.Page(
data = repoResults,
prevKey = if (page == 1) null else page-1,
nextKey = page + 1
)
}.await() as LoadResult<Int, Result>
} catch (e: Exception) {
return LoadResult.Error(e)
}
}
Unfortunately scroll remains slow, so I ask
1.Is it true that load by default runs in a blocking fashion, despite getRepositories and getTopContributor switch to Dispatchers.IO, according to Why ‘withContext’ does not switch coroutines under ‘runBlocking’? it is
2.Since I need to make 1 network request to get repos, and each of those repos need to find their top contributor I am making page_size+1 requests. I’m looking for tips to improve my scroll performance as each page relies on these network requests. My Pager looks like
val pagingData: Flow<PagingData<RepoResult>> = Pager(
config = PagingConfig(pageSize = PAGE_SIZE, prefetchDistance = 4 * PAGE_SIZE),
pagingSourceFactory = { RepoResultPagingSource(repo, QUERY, viewModelScope) }
).flow.cachedIn(viewModelScope)