【问题标题】:How can I know @Insert of Room is completed?我怎么知道@Insert of Room 已经完成?
【发布时间】:2019-01-30 09:47:26
【问题描述】:

我的场景

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

我的问题 我如何知道我的数据库已更新或我的插入已完成,我的删除已完成而不是通过获取大小? @Insert @Delete 是无效的返回方法。那么除了数据库大小我怎么知道呢?

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

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

【问题讨论】:

    标签: kotlin android-room kotlinx.coroutines


    【解决方案1】:

    如果插入的方法调用成功,那么它是成功的,否则你会得到一个异常。

    此外,这些不一定是返回 void 的方法。

    您可以让@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
    

    【讨论】:

    • 非常感谢,我现在正要重新阅读文档,你救救我。干杯!!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    相关资源
    最近更新 更多