gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:46:13 24 4
gpt4 key购买 nike

概览

预期 - 成功进行房间查询 DataSource.Factory<Int, Content>为了填充 PagedList .此策略类似于 Room 🔗 Coroutines Florina Muntenescu 在 Medium 帖子中概述了实现Android Developer Advocate 团队的成员。

已观察到 - 应用无法构建。

错误

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

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

实现

View 模型

  1. ViewModel 使用 viewModelScope发射getContentList() .
  2. getContentList()是一个挂起函数,用另一个挂起函数调用 Repository getMainFeedList() .
  3. LoadingError调用房间的情况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()返回带有 Firebase Firestore 收集请求结果的 LiveData,contentEnCollection.get().addOnCompleteListener.
  3. Firestore 结果保存到带有 insertContentList() 的房间数据库中, 来自嵌套的暂停协程 launch { ... } . <强> insertContentList()使用 suspend 按预期工作.
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.

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

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

实时数据

  • 不需要标记一个返回LiveData的DAO方法。自 ArchTaskExecutor 起可暂停确保查询在 UI 线程外运行,并且结果在 UI 线程上传递。

  • 构建LiveData调用可挂起的方法,你可以使用 liveData {}来自 lifecycle-livedata-ktx 的构建器扩展扩展神器。

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

  • 此外,我不建议返回 MutableLiveData像那样。可变 livedata 通常在私有(private)中发生变异,并作为非可变 LiveData 返回对象。

关于android - Room DataSource.Factory 的 Kotlin 协程挂起错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58424939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com