我想在我的Room DB中存储字符串(uid)。我希望这些uid能够做到:
最有效的方法是什么?
这里是给您一些背景知识的骨架,尽管我对Room的理解非常基础,所以会出现错误:
@Entity(tableName = "user_data")
data class UserData(
@PrimaryKey(autoGenerate = true) val uid: Int,
@ColumnInfo(name = "matched_users") var matchedUsers: Set<String>
)
@Dao
interface UserDataDao {
// Check to see if the uid is in matchedUsers Set/List
@Query("SELECT * FROM user_data WHERE :matchId IN matched_users")
fun matchedBefore(matchId: String): Boolean
// Add uid to Set/List
@Insert(onConflict = OnConflictStrategy.ABORT)
fun addMatchUid(uid: String)
}
任何建议表示赞赏。
答案 0 :(得分:0)
根据您的上下文,您似乎想要建立one-to-many
关系,因此可以遵循以下代码。如果没有,让我知道您到底想要什么。
@Entity(tableName = "user_data")
data class UserData(
@PrimaryKey(autoGenerate = true) val uid: Int,
@ColumnInfo(name = "name") var name: String
)
@Entity(tableName = "user_matcher")
data class UserMatcher(
@PrimaryKey(autoGenerate = true) val uid: Int,
@ColumnInfo(name = "userId") var userId: Int
)
@Dao
interface UserMatcherDao {
// Check to see if the uid is in matchedUsers Set/List
@Query("SELECT m.id as mid, u.id as uid, u.name FROM user_matcher m INNER JOIN user_data u ON m.userId = u.id WHERE m.id = :matchId")
fun getMatchedUsers(matchId: Int): List<UserData>
}