I have an entity where one of the fields is a MutableList. I want to return all the user ids that contain the given ID in that list. The query always return an empty list. If I open the database though I can see that the fields are stored properly and that there are user IDs to be returned. What am I doing wrong?
Data class:
@Entity
data class User(
@PrimaryKey
@SerializedName("id")
@ColumnInfo(name = "userId")
var userId: String,
@SerializedName("username")
var userName: String = "",
var city: String = "",
var postsIds: MutableList<String>
)
Dao:
@Dao
interface UserDao {
@Query("SELECT * FROM user WHERE postsIds LIKE :id")
fun getForPost(id: String): List<User>
// some other queries
}
Repository:
fun getUsersForPost(id: String): LiveData<List<User>> {
val data = MutableLiveData<List<User>>()
GlobalScope.launch {
val query = async(Dispatchers.IO) { userDao.getForPost(id) }
val result = query.await()
if (result.isNullOrEmpty()) {
// todo fetch from the API
} else {
data.postValue(result)
}
}
return data
}
Usage:
ViewModel:
fun getPost(id: String): Post {
val post = repository.getPost(id)
_postEditors.value = repository.getUsersForPost(id).value
return post
}
Fragment:
viewModel.postEditors.observe(this, Observer {
Log.d(TAG, $it)
})