gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:08:43 26 4
gpt4 key购买 nike

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/

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