Android 云Firestore实时数据变化,如实时数据库

Android 云Firestore实时数据变化,如实时数据库,android,firebase-realtime-database,google-cloud-firestore,Android,Firebase Realtime Database,Google Cloud Firestore,我现在正在从事一个项目,该项目使用的是FRDB(firebase实时数据库)。但现在我想切换到云“Firestore”,所以我将用户存储转换为Firestore,并禁用FRDB。但现在在这种情况下,我陷入了困境。我从Firebase上读了一些文档,但作为一个新手,我仍然感到困惑 private fun checkFollowingStatus(uid: String, followButton: Button) { val followingRef = firebaseUser?

我现在正在从事一个项目,该项目使用的是
FRDB
(firebase实时数据库)。但现在我想切换到云“Firestore”,所以我将用户存储转换为
Firestore
,并禁用
FRDB
。但现在在这种情况下,我陷入了困境。我从Firebase上读了一些文档,但作为一个新手,我仍然感到困惑

private fun checkFollowingStatus(uid: String, followButton: Button)
    {
    val followingRef = firebaseUser?.uid.let { it1 ->
        FirebaseDatabase.getInstance().reference
                .child("Follow").child(it1.toString())
                .child("Following")
    }
    followingRef.addValueEventListener(object : ValueEventListener
    {
        override fun onDataChange(datasnapshot: DataSnapshot) {
            if (datasnapshot.child(uid).exists()) {
                followButton.text = "Following"
            }
            else{
                followButton.text = "Follow"
            }
        }
        override fun onCancelled(p0: DatabaseError) {
        }
    })
}
这在
FRDB
中运行得很好,但在数据库转换过程中,我尝试了下面的代码,但在这里卡住了

private fun checkFollowingStatus(uid: String, followButton: Button)
    {
        val followingRef = firebaseUser?.uid.let { it1 ->
            FirebaseFirestore.getInstance()
                    .collection("Follow").document(it1.toString())
                    .collection("Following")
        }
        followingRef.addSnapshotListener(object : EventListener<QuerySnapshot>{
            override fun onEvent(p0: QuerySnapshot?, p1: FirebaseFirestoreException?) {
                if (datasnapshot.child(uid).exists()) { //error: unresolved reference datasnapshot
                    followButton.text = "Following"
                }
                else{
                    followButton.text = "Follow"
                }
            }
        })
    }
private-fun-checkfollowstatus(uid:String,followButton:Button)
{
val followerref=firebaseUser?.uid.let{it1->
FirebaseFirestore.getInstance()
.collection(“Follow”).document(it1.toString())
.收款(“以下”)
}
followerRef.addSnapshotListener(对象:EventListener{
覆盖fun OneEvent(p0:QuerySnapshot?,p1:FirebaseFirestoreException?){
if(datasnapshot.child(uid.exists()){//错误:未解析的引用datasnapshot
followButton.text=“以下”
}
否则{
followButton.text=“Follow”
}
}
})
}

我只是希望当用户单击“跟随”按钮时,用户的uid将存储在以下集合中,并根据按钮的文本进行更改。

看起来您希望检查子集合中是否存在文档

您现有的代码加载整个子集合,并尝试检查一个文档客户端,这是浪费,即使您让它工作

相反,您可以尝试加载单个文档,并检查是否得到结果

val followingRef = firebaseUser?.uid.let { it1 ->
    FirebaseFirestore.getInstance()
            .collection("Follow").document(it1.toString())
            .collection("Following").document(uid)
}
followingRef.addSnapshotListener{ snapshot, e
        if (e != null) {
            Log.w(TAG, "Listen failed.", e)
            return@addSnapshotListener
        }
        if (snapshot != null && snapshot.exists()) { 
            followButton.text = "Following"
        }
        else{
            followButton.text = "Follow"
        }
    }
})
这里还需要注意以下几点:

  • 我所做的更改主要是上的文档的副本。因此,如果出现错误,请务必检查该链接以获取更多信息和示例
  • 实时数据库的原始代码也不需要加载后面的整个
    节点来检查是否存在单个子节点。我强烈建议您检查所有代码以了解这种模式,因为下载的数据超过需要会浪费您和用户的带宽