【问题标题】:Is it possible to return Flow<List<Long>> after Room insert?插入房间后是否可以返回 Flow<List<Long>> ?
【发布时间】:2020-09-23 12:44:56
【问题描述】:
Room插入后是否可以返回Flow<List<Long>>?
类似这样的:
@Insert(onConflict = REPLACE)
suspend fun insert( notesModel: List<NotesModel>): Flow<List<Long>>
当我这样做时,我得到一个异常:
错误:不确定如何处理插入方法的返回类型。
【问题讨论】:
标签:
android
kotlin
android-room
kotlin-coroutines
【解决方案1】:
AFAIK,你不能。 @Insert注解函数的返回类型可以是Unit、Long、List<Long>或Array<Long>。
只有 @Query 注释函数能够返回可观察对象,例如 LivaData、Flow、Single、Flowable、等。因为@Query 函数被设计为能够反映db 上的变化。
这是来自official documentation的引用:
如果@Insert 方法只接收1 个参数,它可以返回一个long,即插入项的新rowId。如果参数是数组或集合,则应返回 long[] 或 List。