首页 > 解决方案 > 如何在 Firestore 中构建关注系统

问题描述

我从 Firebase 实时数据库切换到 Firestore 在实时数据库中,我将此代码用于以下系统:

checkFollowingStatus(user.getUID(), holder.followButton)
holder.followButton.setOnClickListener {
            if(holder.followButton.text.toString() == "Follow")
            {
                firebaseUser?.uid.let { it1 ->
                    FirebaseDatabase.getInstance().reference
                            .child("Follow").child(it1.toString())
                            .child("Following").child(user.getUID())
                            .setValue(true).addOnCompleteListener { task ->
                                if (task.isSuccessful) {
                                    firebaseUser?.uid.let { it ->
                                        FirebaseDatabase.getInstance().reference
                                                .child("Follow").child(user.getUID())
                                                .child("Followers").child(it.toString())
                                                .setValue(true).addOnCompleteListener { task ->
                                                    if (task.isSuccessful) {

                                                    }
                                                }
                                    }
                                }
                            }
                }
            }
            else
            {
                firebaseUser?.uid.let { it1 ->
                    FirebaseDatabase.getInstance().reference
                            .child("Follow").child(it1.toString())
                            .child("Following").child(user.getUID())
                            .removeValue().addOnCompleteListener { task ->
                                if (task.isSuccessful) {
                                    firebaseUser?.uid.let { it1 ->
                                        FirebaseDatabase.getInstance().reference
                                                .child("Follow").child(user.getUID())
                                                .child("Followers").child(it1.toString())
                                                .removeValue().addOnCompleteListener { task ->
                                                    if (task.isSuccessful) {

                                                    }
                                                }
                                    }
                                }
                            }
                }
            } }
       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 = "Unfollow"
            } else {
                followButton.text = "Follow"
            }
        }

        override fun onCancelled(p0: DatabaseError) {

        }
    })

}

它在实时数据库中看起来像这样
+Follow
   +userId
     +Following
        +usersId...
     +Followers
        +usersId...
如何将其转换为 Firestore 或如何在 Firestore 数据库中构建关注系统?

标签: javaandroidfirebasekotlingoogle-cloud-firestore

解决方案


var hashMap : HashMap<String, Object> 
           = HashMap<String, Object> () 

hashMap.put("someName", true);


并将此哈希映射传递给 set 方法

databaseRefernce.addSnapshotListener(new EventListener<DocumentSnapshot or QuerySnapshot>() {

    @Override
    public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
      if (value.exists())  {
      }
    }

});

推荐阅读