首页 > 解决方案 > 如何检查firestore中的条目列表

问题描述

我正在尝试制作一个应用程序,在该应用程序中我必须只显示那些存在于 firestore 中的用户联系人,我当前的逻辑是从设备中获取所有联系人,然后在 firestore 中单独检查它们并更新数据适配器。

但我想要做的是获取所有设备联系人,一旦所有设备都被查找,然后只更新适配器,但使用 firestore get() 我无法做到这一点,有没有办法做到这一点?

我当前的代码

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()
                }
            }
        }
    }

我想要的是

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()
    }

标签: androidfirebasegoogle-cloud-firestore

解决方案


我不知道它是否完美,但您可以在 Firebase 中为您放置与用户联系人匹配的合同的每个用户添加一个节点。第一次检查所有联系人并将其放在节点上。然后你可以从那里得到它。然后您可以在后台更新节点,Firebase 将实时给出更新结果。


推荐阅读