For example, I have a list of issues. Each issue has owner uid. By this uid, I should find needed users and display his name and photo. I do it with help architecture component ViewModel:
issues.forEach {
IssueRepository().getIssueOwner(it.owner).observe(this, Observer {
})
}
getIssueOwner method:
fun getIssueOwner(uid: String): MutableLiveData<UserEntity> {
val user: MutableLiveData<UserEntity> = MutableLiveData()
val usersReference = FirebaseDatabase.getInstance().reference.child("users")
val query = usersReference.orderByKey().equalTo(uid).limitToFirst(1)
query.addValueEventListener(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError?) {
}
override fun onDataChange(dataSnapshot: DataSnapshot?) {
if (dataSnapshot == null) {
return
}
dataSnapshot.children.forEach {
val name = it.child("displayName").value
user.postValue(UserEntity(name))
}
}
})
return user
}
But I'm sure that this approach is not correct. Could you please give me an advice how I should build the architecture of my app?