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)
。我不明白您的意思……我不明白您的意思……请帮助,这个问题没有人帮助请帮助,这个问题没有人帮助