如何在 Kotlin 的 android 房间数据库中获取插入行的 ID?
How to get the id of inserted row in android room databse in Kotlin?
我正在尝试从最新用户那里获取用户 ID。如何在自动生成 ID 时让插入方法吐出 ID?
模型
@PrimaryKey(autoGenerate = true)
val userId: Int
在道中
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun addUserWithLong(user: User): LiveData<Long>
在存储库中
fun addUserWitLong(user: User): LiveData<Long> {
return userDao.addUserWithLong(user)
}
在 ViewModel 中
fun addUserWithLong(user: User): LiveData<Long> {
return repository.addUserWitLong(user)
}
在片段中
val id: Long? = userViewModel.addUserWithLong(user).value
我在文档中读到 @Insert returns Long 作为行 ID,但我不知道如何对其进行编程。现在错误是“不确定如何处理插入方法 return 类型。”有什么方法可以用 LiveData 而不是用 Rxjava 来制作。那是不需要下载更多的依赖项。
根据文档here
If the @Insert method receives a single parameter, it can return a
long value, which is the new rowId for the inserted item. If the
parameter is an array or a collection, then the method should return
an array or a collection of long values instead, with each value as
the rowId for one of the inserted items. To learn more about returning
rowId values, see the reference documentation for the @Insert
annotation, as well as the SQLite documentation for rowid tables
所以你可以像这样使用它
@Insert(onConflict = OnConflictStrategy.REPLACE)
long addUserWithLong(user: User)
或者如果您要插入列表
@Insert(onConflict = OnConflictStrategy.REPLACE)
long[] addUserWithLong(user: List<User>)
编辑-1
检查 post.
的答案后
No, you can't. I wrote an answer to the issue. The reason is, that
LiveData is used to notify for changes. Insert, Update, Delete won't
trigger a change.
我刚刚创建了一个测试项目,并成功收到了 activity 中最后插入的项目的 ID。这是我的实现。
道
@Insert
suspend fun addUser(user: Users): Long
回购
suspend fun insertUser(context: Context, users: Users): Long {
val db = AppDatabase.getInstance(context)
val dao = db.userDao()
return dao.addUser(users)
}
ViewModel
fun addUser(context: Context, users: Users) = liveData {
//you can also emit your customized object here.
emit("Inserting...")
try {
val userRepo = UsersRepo()
val response = userRepo.insertUser(context, users)
emit(response)
} catch (e: Exception) {
e.printStackTrace()
emit(e.message)
}
}
Activity
viewModel.addUser(applicationContext, user).observe(this, Observer { userId ->
Log.d("MainActivity", "Inserted User Id is $userId")
})
检查测试应用程序 here.
我正在尝试从最新用户那里获取用户 ID。如何在自动生成 ID 时让插入方法吐出 ID?
模型
@PrimaryKey(autoGenerate = true)
val userId: Int
在道中
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun addUserWithLong(user: User): LiveData<Long>
在存储库中
fun addUserWitLong(user: User): LiveData<Long> {
return userDao.addUserWithLong(user)
}
在 ViewModel 中
fun addUserWithLong(user: User): LiveData<Long> {
return repository.addUserWitLong(user)
}
在片段中
val id: Long? = userViewModel.addUserWithLong(user).value
我在文档中读到 @Insert returns Long 作为行 ID,但我不知道如何对其进行编程。现在错误是“不确定如何处理插入方法 return 类型。”有什么方法可以用 LiveData 而不是用 Rxjava 来制作。那是不需要下载更多的依赖项。
根据文档here
If the @Insert method receives a single parameter, it can return a long value, which is the new rowId for the inserted item. If the parameter is an array or a collection, then the method should return an array or a collection of long values instead, with each value as the rowId for one of the inserted items. To learn more about returning rowId values, see the reference documentation for the @Insert annotation, as well as the SQLite documentation for rowid tables
所以你可以像这样使用它
@Insert(onConflict = OnConflictStrategy.REPLACE)
long addUserWithLong(user: User)
或者如果您要插入列表
@Insert(onConflict = OnConflictStrategy.REPLACE)
long[] addUserWithLong(user: List<User>)
编辑-1
检查
No, you can't. I wrote an answer to the issue. The reason is, that LiveData is used to notify for changes. Insert, Update, Delete won't trigger a change.
我刚刚创建了一个测试项目,并成功收到了 activity 中最后插入的项目的 ID。这是我的实现。
道
@Insert
suspend fun addUser(user: Users): Long
回购
suspend fun insertUser(context: Context, users: Users): Long {
val db = AppDatabase.getInstance(context)
val dao = db.userDao()
return dao.addUser(users)
}
ViewModel
fun addUser(context: Context, users: Users) = liveData {
//you can also emit your customized object here.
emit("Inserting...")
try {
val userRepo = UsersRepo()
val response = userRepo.insertUser(context, users)
emit(response)
} catch (e: Exception) {
e.printStackTrace()
emit(e.message)
}
}
Activity
viewModel.addUser(applicationContext, user).observe(this, Observer { userId ->
Log.d("MainActivity", "Inserted User Id is $userId")
})
检查测试应用程序 here.