Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 刷新房间数据库中的项目_Android_Sqlite_Kotlin_Android Room - Fatal编程技术网

Android 刷新房间数据库中的项目

Android 刷新房间数据库中的项目,android,sqlite,kotlin,android-room,Android,Sqlite,Kotlin,Android Room,我有一个应用程序,它使用房间数据库将我的“朋友”作为帐户存储在帐户表中 @Entity(tableName = "accounts") data class Account( @PrimaryKey @ColumnInfo(name = "account_id") val accountId: Int, @ColumnInfo(name = "first_name", defaultValue = &

我有一个应用程序,它使用房间数据库将我的“朋友”作为帐户存储在帐户表中

@Entity(tableName = "accounts")
data class Account(

    @PrimaryKey
    @ColumnInfo(name = "account_id")
    val accountId: Int,

    @ColumnInfo(name = "first_name", defaultValue = "")
    var firstname: String,

    @ColumnInfo(name = "last_name", defaultValue = "")
    var lastname: String,

    @ColumnInfo(name = "email", defaultValue = "")
    var email: String,

    @ColumnInfo(name = "status")
    var status: Int,

    @ColumnInfo(name = "role_id")
    var roleId: Int,

    @ColumnInfo(name = "lang", defaultValue = "")
    var lang: String

) : Serializable
因此,当我刷新我的帐户时,可能会有帐户

  • 将被删除
  • 将插入
  • 将进行更新

  • 确定哪些记录需要执行哪些操作以及如何执行这些操作的最佳方法是什么?

    假设您有新帐户:

    val newList: List<Account> = ... //
    
    val newList:List=//
    
    您可以在Dao中放置下一个方法:

    // To delete all accounts that are not included in new list
    @Query("DELETE FROM account WHERE accountId NOT IN (: newIds)")
    suspend fun deleteOutdatedAccounts(newIds: List<Int>)
    
    // To insert/update all accounts from the new list
    // Thanks to OnConflictStrategy.REPLACE strategy you get both insert and update
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertUpdateAccounts(accounts: List<Account>)
    
    // To do two methods above in single transaction
    @Transaction
    suspend fun refreshAccounts(newList List<Account>){
        deleteOutdatedAccounts(newList.map{it.accountId})
        insertUpdateAccounts(newList)    
    }
    
    //删除未包含在新列表中的所有帐户
    @查询(“从accountId不在(:newId)中的帐户中删除”)
    suspend fun deleteOutdatedAccounts(新ID:列表)
    //插入/更新新列表中的所有帐户
    //由于OnConflictStrategy.REPLACE策略,您可以同时获得插入和更新
    @插入(onConflict=OnConflictStrategy.REPLACE)
    挂起fun InsertUpdate帐户(帐户:列表)
    //在单个事务中执行上述两种方法
    @交易
    暂停帐户(新列表){
    deleteOutdatedAccounts(newList.map{it.accountId})
    插入更新帐户(新列表)
    }
    

    之后,您可以从存储库或ViewModel/Presenter调用方法
    refreshAccounts(newList)

    假设您有新帐户:

    val newList: List<Account> = ... //
    
    val newList:List=//
    
    您可以在Dao中放置下一个方法:

    // To delete all accounts that are not included in new list
    @Query("DELETE FROM account WHERE accountId NOT IN (: newIds)")
    suspend fun deleteOutdatedAccounts(newIds: List<Int>)
    
    // To insert/update all accounts from the new list
    // Thanks to OnConflictStrategy.REPLACE strategy you get both insert and update
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertUpdateAccounts(accounts: List<Account>)
    
    // To do two methods above in single transaction
    @Transaction
    suspend fun refreshAccounts(newList List<Account>){
        deleteOutdatedAccounts(newList.map{it.accountId})
        insertUpdateAccounts(newList)    
    }
    
    //删除未包含在新列表中的所有帐户
    @查询(“从accountId不在(:newId)中的帐户中删除”)
    suspend fun deleteOutdatedAccounts(新ID:列表)
    //插入/更新新列表中的所有帐户
    //由于OnConflictStrategy.REPLACE策略,您可以同时获得插入和更新
    @插入(onConflict=OnConflictStrategy.REPLACE)
    挂起fun InsertUpdate帐户(帐户:列表)
    //在单个事务中执行上述两种方法
    @交易
    暂停帐户(新列表){
    deleteOutdatedAccounts(newList.map{it.accountId})
    插入更新帐户(新列表)
    }
    

    之后,您可以从存储库或ViewModel/Presenter调用方法
    refreshAccounts(newList)

    我不明白您的意思……我不明白您的意思……请帮助,这个问题没有人帮助请帮助,这个问题没有人帮助