kotlin - 我怎么知道房间的@Insert 已完成?

标签 kotlin android-room kotlinx.coroutines

My scenario



我使用 Coroutines 和 Room 来保存我的应用程序的用户配置文件数据。我有 CompleteProfileActivity :在那个用户填写他们的信息并确认它(确认按钮)。我将它们发送到服务器并观察响应。如果响应成功。我将它们保存到我的 ProfileDatabase。

My question How can I know my database is updated or my insertion is complete, my deletion is completed not by getting the size? @Insert @Delete is void return methods. So how can I know except the database size?


@Dao
interface ProfileDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun saveProfile(model:Profile)

@Query("SELECT * FROM profile_table")
fun getProfile():Deferred<Profile>
}

最佳答案

如果插入的方法调用成功,则说明成功,否则会出现异常。

此外,这些不必是返回空值的方法。

您可以拥有 @Insert返回表中主键的类型,这将是新插入记录的键。

@Insert(onConflict = OnConflictStrategy.IGNORE)
fun saveProfile(model: Profile): Int

如果是 @Delete , 如果您返回 Int ,它将返回删除的行数:
@Delete
fun deleteProfiles(profiles: List<Profile>): Int

使用 @Query 进行更手动的实现也是如此, 如果返回 Int,则返回受影响的行数:
@Query("DELETE FROM profiles")
fun deleteAllProfiles(): Int

关于kotlin - 我怎么知道房间的@Insert 已完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54437576/

相关文章:

android - 房间选择表达式

kotlin - TypeConverters 无法弄清楚如何将此字段保存到数据库中

spring - 为什么 Spring Boot 2.7.x 不使用 kotlin 1.7 或 1.8

androidx.navigation.fragment.NavHostFragment 无法从 xml 文件访问

android - 当 2d 数组元素的变量在 Compose with Clean Architecture 中发生变化时重新组合

android - Dagger 2 - 在构造函数中注入(inject)默认值

Android Room 无法弄清楚如何将此字段保存到数据库中

kotlin - 如何将制作人与 Actor 联系起来?

Kotlin 协程 GlobalScope.launch 与 runBlocking

android - kotlin coroutine throws java.lang.IllegalStateException : Already resumed, 但得到值 Location