android - 是否可以将 Flow 与 Room 删除或插入操作一起使用,就像在 RxJava 中使用 Single 或 Maybe 返回行 id 一样?

标签 android android-room kotlin-flow

调用

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(entity: PostEntity): Flow<Long>
   
@Delete
fun deletePost(entity: PostEntity): Flow<Long>

返回编译错误

 Not sure how to handle insert method's return type.
    public abstract kotlinx.coroutines.flow.Flow<java.util.List<java.lang.Long>> insertFlow(@org.jetbrains.annotations.NotNull()

是否可以像 RxJava 一样使用 Flow 与 Int 或 long 来获取数据库操作结果的 id?

@Insert(onConflict = REPLACE)
fun insert(entity: T): Maybe<Long>

@Insert(onConflict = REPLACE)
fun insert(entity: T): Single<Long>

最佳答案

根据 Async queries with Kotlin coroutines in Room ,Kotlin 协程中 MaybeSingle 的等效项是 suspend:

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(entity: PostEntity): Long

@Delete
suspend fun deletePost(entity: PostEntity): Long

关于android - 是否可以将 Flow 与 Room 删除或插入操作一起使用,就像在 RxJava 中使用 Single 或 Maybe 返回行 id 一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63308455/

相关文章:

java - 从 Res 文件夹中读取文件

android - 无法为类型为 'packageForR' 的任务获取未知属性 '::processDebugResources'

Android:在另一个不发射的情况下收集 Kotlin Flow

unit-testing - 使用协程进行测试时检测到使用不同的调度程序

android - 从拍摄的视频的内容 uri 获取文件

java - 在 Android 上使用管道读取 shell 命令的输出

android - 如何使用列表大小与 Room 数据库返回的列表大小不同的 AAC 分页库

android - 在仪器测试中找不到 Room.inMemoryDatabaseBuilder()

Android - 如何从小部件访问房间数据库

android - 有流的房间在空时返回 null