From d37c5e50f0080bae6a58fe48029b6d3f71579ca9 Mon Sep 17 00:00:00 2001 From: ruffCode Date: Tue, 28 Jul 2020 18:52:13 -0400 Subject: [PATCH] fixes #93 On initial app start, GithubRemoteMediator's load function is called with LoadType.REFRESH, the keys and repos are saved to Room. When it is immediately called again with LoadType.PREPEND, the call to getRemoteKeyForFirstItem returns null and InvalidObjectException is thrown. On app restart, the keys are present and no errors are thrown --- .../android/codelabs/paging/data/GithubRemoteMediator.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/example/android/codelabs/paging/data/GithubRemoteMediator.kt b/app/src/main/java/com/example/android/codelabs/paging/data/GithubRemoteMediator.kt index efb34176..99b27bac 100644 --- a/app/src/main/java/com/example/android/codelabs/paging/data/GithubRemoteMediator.kt +++ b/app/src/main/java/com/example/android/codelabs/paging/data/GithubRemoteMediator.kt @@ -26,6 +26,10 @@ import com.example.android.codelabs.paging.api.IN_QUALIFIER import com.example.android.codelabs.paging.db.RemoteKeys import com.example.android.codelabs.paging.db.RepoDatabase import com.example.android.codelabs.paging.model.Repo +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.flowOn +import kotlinx.coroutines.flow.single import retrofit2.HttpException import java.io.IOException import java.io.InvalidObjectException @@ -48,7 +52,8 @@ class GithubRemoteMediator( remoteKeys?.nextKey?.minus(1) ?: GITHUB_STARTING_PAGE_INDEX } LoadType.PREPEND -> { - val remoteKeys = getRemoteKeyForFirstItem(state) + val remoteKeys = flowOf(getRemoteKeyForFirstItem(state)) + .flowOn(Dispatchers.IO).single() if (remoteKeys == null) { // The LoadType is PREPEND so some data was loaded before, // so we should have been able to get remote keys