Room DataSource.Factory 的 Kotlin 协程挂起错误

Kotlin coroutine suspend error with Room DataSource.Factory

概述

预期 - 成功对 DataSource.Factory<Int, Content> 进行房间查询以填充 PagedList。此策略类似于 Android Developer Advocate 团队的 Room Coroutines implementation outlined in the Medium post by Florina Muntenescu

观察到 - 应用构建失败。

错误

遗憾的是,没有更具体的错误提示问题的根源。

A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution

实施

ViewModel

  1. ViewModel 使用 viewModelScope 启动 getContentList()
  2. getContentList()是一个挂起函数,用另一个挂起函数getMainFeedList().
  3. 调用Repository
  4. LoadingError 的情况下,调用 Room queryMainContentList(...)
class ContentViewModel : ViewModel() {
    fun processEvent(...) {
        ...
        viewModelScope.launch {
            _feedViewState.value = FeedViewState(contentList = getContentList(...))
        }
        ...
    }

    suspend private fun getContentList(...): LiveData<PagedList<Content>> =
            switchMap(getMainFeedList(isRealtime, timeframe)) { lce ->
                when (lce) {
                    is Loading -> 
                       coroutineScope { 
                          emitSource(queryMainContentList(...)) 
                       }
                    is Lce.Content -> lce.packet.pagedList!!
                    is Error -> 
                       coroutineScope { 
                          emitSource(queryMainContentList(...)) 
                       }
                }
            }
}

存储库

  1. getMainFeedList() 是一个挂起函数,它使用 withContext(Dispatchers.Default) 来获取协程作用域。
  2. getMainFeedList() returns LiveData 以及来自 Firebase Firestore 收集请求的结果,contentEnCollection.get().addOnCompleteListener.
  3. Firestore 结果从嵌套的暂停协程 launch { ... } 中保存到带有 insertContentList() 的 Room DB。 insertContentList() 使用 suspend.
  4. 按预期工作
object ContentRepository {
    fun getMainFeedList(...) =  liveData<Lce<PagedListResult>> {
            val lce = this
            val newContentList = arrayListOf<Content?>()
            contentEnCollection.get().addOnCompleteListener {
                arrayListOf<Content?>().also { contentList ->
                    it.result!!.documents.all { document ->
                        contentList.add(document.toObject(Content::class.java))
                        true
                    }
                    newContentList.addAll(contentList)
                }
                CoroutineScope(Dispatchers.Default).launch {
                   try {
                      database.contentDao().insertContentList(newContentList)
                   } catch (e: Exception) {
                      this.cancel()
                   }
                }.invokeOnCompletion { throwable ->
                   if (throwable == null)
                      lce.emit(Lce.Content(PagedListResult(
                         pagedList = queryMainContentList(timeframe),
                         errorMessage =  "")))
                   else // Log Room error.
                }
            }.addOnFailureListener {
                // Log Firestore error here.
                lce.emit(...)
            }
        }
    }
}

suspend fun queryMainContentList(timestamp: Timestamp) =
            liveDataBuilder(database.contentDao().queryMainContentList(timestamp, MAIN))

fun liveDataBuilder(dataSource: DataSource.Factory<Int, Content>) =
        LivePagedListBuilder(dataSource,
                PagedList.Config.Builder().setEnablePlaceholders(true)
                        .setPrefetchDistance(PREFETCH_DISTANCE)
                        .setPageSize(PAGE_SIZE)
                        .build())
                .build()

insertContentList() 按预期工作。

@Dao
interface ContentDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertContentList(users: ArrayList<Content?>)

    @Query("SELECT * FROM content WHERE timestamp >= :timeframe AND feedType = :feedType ORDER BY timestamp DESC")
    suspend fun queryMainContentList(timeframe: Timestamp, feedType: FeedType): DataSource.Factory<Int, Content>
}

堆栈跟踪

重要的部分似乎是以下内容:

error: Not sure how to convert a Cursor to this method's return type (androidx.paging.DataSource.Factory).

这是完整的日志:

21:50:30: Executing task 'assembleAndroidTest'...

Executing tasks: [assembleAndroidTest] in project /Users/adamhurwitz/Coinverse/android

Configure project :app WARNING: The following project options are deprecated and have been removed: android.databinding.enableV2 Databinding v1 is removed.

WARNING: The option setting 'android.enableR8.fullMode=true' is experimental and unsupported. The current default is 'false'.

WARNING: API 'variant.getAssemble()' is obsolete and has been replaced with 'variant.getAssembleProvider()'. It will be removed at the end of 2019. For more information, see https://d.android.com/r/tools/task-configuration-avoidance. To determine what is calling variant.getAssemble(), use -Pandroid.debug.obsoleteApi=true on the command line to display more information. WARNING: API 'variantOutput.getProcessResources()' is obsolete and has been replaced with 'variantOutput.getProcessResourcesProvider()'. It will be removed at the end of 2019. For more information, see https://d.android.com/r/tools/task-configuration-avoidance. To determine what is calling variantOutput.getProcessResources(), use -Pandroid.debug.obsoleteApi=true on the command line to display more information. WARNING: API 'variantOutput.getProcessManifest()' is obsolete and has been replaced with 'variantOutput.getProcessManifestProvider()'. It will be removed at the end of 2019. For more information, see https://d.android.com/r/tools/task-configuration-avoidance. To determine what is calling variantOutput.getProcessManifest(), use -Pandroid.debug.obsoleteApi=true on the command line to display more information. WARNING: API 'variant.getMergeResources()' is obsolete and has been replaced with 'variant.getMergeResourcesProvider()'. It will be removed at the end of 2019. For more information, see https://d.android.com/r/tools/task-configuration-avoidance. To determine what is calling variant.getMergeResources(), use -Pandroid.debug.obsoleteApi=true on the command line to display more information. WARNING: API 'variant.getMergeAssets()' is obsolete and has been replaced with 'variant.getMergeAssetsProvider()'. It will be removed at the end of 2019. For more information, see https://d.android.com/r/tools/task-configuration-avoidance. To determine what is calling variant.getMergeAssets(), use -Pandroid.debug.obsoleteApi=true on the command line to display more information. WARNING: API 'variant.getPackageApplication()' is obsolete and has been replaced with 'variant.getPackageApplicationProvider()'. It will be removed at the end of 2019. For more information, see https://d.android.com/r/tools/task-configuration-avoidance. To determine what is calling variant.getPackageApplication(), use -Pandroid.debug.obsoleteApi=true on the command line to display more information. WARNING: API 'variant.getExternalNativeBuildTasks()' is obsolete and has been replaced with 'variant.getExternalNativeBuildProviders()'. It will be removed at the end of 2019. For more information, see https://d.android.com/r/tools/task-configuration-avoidance. To determine what is calling variant.getExternalNativeBuildTasks(), use -Pandroid.debug.obsoleteApi=true on the command line to display more information.

Task :app:preBuild UP-TO-DATE Task :app:preDebugBuild UP-TO-DATE Task :app:mergeDebugShaders UP-TO-DATE Task :app:compileDebugShaders UP-TO-DATE Task :app:generateDebugAssets UP-TO-DATE Task :app:processDebugGoogleServices UP-TO-DATE Task :app:checkDebugManifest UP-TO-DATE Task :app:createDebugCompatibleScreenManifests UP-TO-DATE Task :app:mainApkListPersistenceDebug UP-TO-DATE Task :app:generateDebugBuildConfig UP-TO-DATE Task :app:compileDebugAidl NO-SOURCE Task :app:compileDebugRenderscript NO-SOURCE Task :app:mergeDebugAssets UP-TO-DATE Task :app:processDebugManifest UP-TO-DATE Task :app:fabricGenerateResourcesDebug Task :app:writeDebugApplicationId UP-TO-DATE Task :app:generateSafeArgsDebug UP-TO-DATE Task :app:prepareLintJar UP-TO-DATE Task :app:prepareLintJarForPublish UP-TO-DATE Task :app:generateDebugSources Task :app:dataBindingExportBuildInfoDebug UP-TO-DATE Task :app:dataBindingMergeDependencyArtifactsDebug UP-TO-DATE Task :app:generateDebugResValues UP-TO-DATE Task :app:dataBindingMergeGenClassesDebug UP-TO-DATE Task :app:generateDebugResources UP-TO-DATE Task :app:dataBindingExportFeaturePackageIdsDebug UP-TO-DATE Task :app:preDebugAndroidTestBuild SKIPPED Task :app:compileDebugAndroidTestAidl NO-SOURCE Task :app:processDebugAndroidTestManifest UP-TO-DATE Task :app:compileDebugAndroidTestRenderscript NO-SOURCE Task :app:generateDebugAndroidTestBuildConfig UP-TO-DATE Task :app:mainApkListPersistenceDebugAndroidTest UP-TO-DATE Task :app:generateDebugAndroidTestResValues UP-TO-DATE Task :app:generateDebugAndroidTestResources UP-TO-DATE Task :app:mergeDebugAndroidTestResources UP-TO-DATE Task :app:processDebugAndroidTestResources UP-TO-DATE Task :app:mergeDebugAndroidTestShaders UP-TO-DATE Task :app:compileDebugAndroidTestShaders UP-TO-DATE Task :app:generateDebugAndroidTestAssets UP-TO-DATE Task :app:mergeDebugAndroidTestAssets UP-TO-DATE Task :app:processDebugAndroidTestJavaRes NO-SOURCE Task :app:mergeDebugAndroidTestJniLibFolders UP-TO-DATE Task :app:mergeDebugAndroidTestNativeLibs UP-TO-DATE Task :app:checkDebugAndroidTestDuplicateClasses UP-TO-DATE Task :app:validateSigningDebugAndroidTest UP-TO-DATE Task :app:signingConfigWriterDebugAndroidTest UP-TO-DATE /Users/adamhurwitz/Coinverse/android/app/build/intermediates/incremental/mergeDebugResources/merged.dir/values/values.xml:1002: warn: multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?. /Users/adamhurwitz/Coinverse/android/app/build/intermediates/incremental/mergeDebugResources/merged.dir/values/values.xml:1031: warn: multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?.

Task :app:mergeDebugResources Task :app:dataBindingGenBaseClassesDebug UP-TO-DATE Task :app:processDebugResources Task :app:kaptGenerateStubsDebugKotlin UP-TO-DATE

Task :app:kaptDebugKotlin FAILED ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1/Users/adamhurwitz/Coinverse/android/app/build/tmp/kapt3/stubs/debug/app/coinverse/content/room/ContentDao.java:17: error: Not sure how to convert a Cursor to this method's return type (androidx.paging.DataSource.Factory). public abstract java.lang.Object queryLabeledContentList(@org.jetbrains.annotations.NotNull() Note: 1 Wrote GeneratedAppGlideModule with: [][WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.lifecycle.LifecycleProcessor (NON_INCREMENTAL). ^ FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:kaptDebugKotlin'. A failure occurred while executing > org.jetbrains.kotlin.gradle.internal.KaptExecution java.lang.reflect.InvocationTargetException (no error message)

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 9s 38 actionable tasks: 4 executed, 34 up-to-date 21:50:40: Task execution finished 'assembleAndroidTest'.

分页列表

A PagedList 默认处理后台线程上的加载数据。

If you use LivePagedListBuilder to get a LiveData, it will initialize PagedLists on a background thread for you.

  • 不需要将产生DataSource.Factory的Dao方法标记为suspend。 return 类型被注释处理器用来知道生成什么样的代码,就像 suspend 修饰符一样。

  • A DataSourceLiveData<PagedList> 一起使用时将在主线程外执行查询以安全地调用不可暂停的数据库和阻塞网络调用。

实时数据

  • 不需要将 returns LiveData 的 DAO 方法标记为可暂停,因为 ArchTaskExecutor 确保查询是 运行 关闭 UI 线程,结果在 UI 线程上传递。

  • 要构建调用可挂起方法的 LiveData,您可以使用 lifecycle-livedata-ktx 扩展工件中的 liveData {} 构建器扩展。

  • 还有一个 LiveData-builder 可以直接从您的 DataSource.Factory 构建 LiveData<PagedList>,查看 paging-runtime-ktx 工件。

  • 此外,我不建议像那样 returning MutableLiveData。可变的 livedata 通常是私有的,并且 returned 作为一个非可变的 LiveData 对象。