Reputation: 31
I am trying to make an application in which I have to show only those user contacts which are there in firestore, my current logic is to fetch all contacts from device and then individually check for them in firestore and update data adapter.
But what I want to do is get all device contacts and once all of them are looked up then only update adapter but with firestore get() I am unable to do this, Is there a way to do this ?
My Current Code
private fun refreshContacts() {
val list = getContacts() // this return a list of all contacts on device
adapter.deleteAllData() // delete current data
list.forEach { // for each contact check if it is in firestore
firestore.collection("users").document(it.number)
.get().addOnSuccessListener { res ->
if (res.exists()) {
val image = res.getString("image")
val uid = res.getString("uid")
val model = ContactsModel(it.name, it.number, uid!!, image!!)
adapter.updateData(model)
adapter.notifyDataSetChanged()
}
}
}
}
What I want
private fun refreshContacts() {
val list = getContacts() // this return a list of all contacts on device
adapter.deleteAllData() // delete current data
// code which gives a list of contacts that were present in firestore
adapter.updateData(newData)
aapter.notifyDataSetChanged()
}
Upvotes: 0
Views: 56
Reputation: 574
I don't know its perfect or not but you can add a node in Firebase for each user where you put that contracts which is matched with users contacts.For the first time check all contacts and put it at the node. Then you can get it from there. You can then update the node in background and Firebase will give update result in Real time.
Upvotes: 1