我有一个类似 Twitter 的社交应用程序,当用户寻找另一个用户时,我想看看登录用户是否关注另一个用户,但我不知道如何遍历并找出登录用户是否已经关注了另一个。
{
"follower" : {
"mt0fzirhMhazIcy90MRWuRpTfmE2" : {
"-KbHXdeiOfGXzvavuQ_5" : {
"uid" : "dEXaVLDOSPfJa3zTyUNqAEtVuMR2"
}
}
},
"following" : {
"dEXaVLDOSPfJa3zTyUNqAEtVuMR2" : {
"-KbHXdehbkeMvDyzNpRE" : {
"uid" : "mt0fzirhMhazIcy90MRWuRpTfmE2"
}
}
},
"handles" : {
"jcadmin" : "mt0fzirhMhazIcy90MRWuRpTfmE2",
"jcbest" : "dEXaVLDOSPfJa3zTyUNqAEtVuMR2"
},
"user_profiles" : {
"dEXaVLDOSPfJa3zTyUNqAEtVuMR2" : {
"about" : "Hello world",
"handle" : "jcbest",
"name" : "Juan Carlos Estevez Rodriguez",
"profile_pic" : "https://firebasestorage.googleapis.com/v0/b..."
},
"mt0fzirhMhazIcy90MRWuRpTfmE2" : {
"about" : "Hello",
"handle" : "jcadmin",
"name" : "Juan Carlos",
"profile_pic" : "https://firebasestorage.googleapis.com/v0..."
}
}
}
这是我的数据库,我想要的是这样的
self.databaseRef.child("following").child((self.loggedInUser?.uid)!).observe(.value, with: { (snapshot) in
let snapshot = snapshot.value as? [String: AnyObject]
if("The other user UID exists in the following")
{
self.followButton.tittleLabel = "Unfollow"
}
})
最佳答案
你不需要$autoId
,你可以只使用follower/following user的$uid
作为key。
"follower" : {
"mt0fzirhMhazIcy90MRWuRpTfmE2" : {
"dEXaVLDOSPfJa3zTyUNqAEtVuMR2": true
}
},
"following" : {
"dEXaVLDOSPfJa3zTyUNqAEtVuMR2" : {
"mt0fzirhMhazIcy90MRWuRpTfmE2": true
}
},
然后可以观察/following/$uid/$userId的值,切换快照值来判断是Bool
等于true
还是只是空
。
let uid = FIRAuth.auth()!.currentUser!.uid
let ref = FIRDatabase.database().reference(
withPath: "following/\(uid)/$userId"
)
ref.observe(.value, with: { (snapshot) in
switch snapshot.value {
case let value as Bool where value:
// value was a Bool and equal to true
self.followButton.tittleLabel = "Unfollow"
default:
// value was either null, false or could not be cast
self.followButton.tittleLabel = "Follow"
}
})
关于ios - 如何在 Firebase 中循环访问 AutoIdChilds?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41840163/