java - 如何在 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 数据库中构建关注系统?
解决方案
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()) {
}
}
});
推荐阅读
- c# - 尽管有足够的参数,SQLite 仍显示“提供给命令的参数不足”
- javascript - 如何从 React JS 中的其他兄弟组件传递 props 值
- python - 纯python或itertools按每个日期之间的天数对日期列表进行分组
- css - 材质ui MaxwidthLG
- reactjs - 如何在 Razorpay 中找到付款详情(如付款方式、支付金额等)?
- android - 房间数据库:插入数据后如何获得响应?
- javascript - 使用 react JS 打印收据
- postgresql - 如何在预定时间获得 postgresql 与 pgcenter 的活动连接?
- javascript - 带有 jquery 或 ajax 的烧瓶返回 {"readyState":0,"status":0,"statusText":"error"}
- c# - 在 c# 中识别 iPad ios13 和 MAC OS 之间的设备