Ios 检索用户根集合引用同级集合的firebase数据
使用此路径访问所有地图在Firebase中都能完美工作,但是它会获取“地图”根集合中可用的所有地图Ios 检索用户根集合引用同级集合的firebase数据,ios,swift,firebase,Ios,Swift,Firebase,使用此路径访问所有地图在Firebase中都能完美工作,但是它会获取“地图”根集合中可用的所有地图 让您的mapref=Database.Database().reference().child(“映射”) 我试图只访问用户所属的地图。因此,我试着跟随堆栈问题和firebase教程,但我无法掌握如何做到这一点 例如,我想亚当只抓取他的版主地图 让yourMapRef=Database.Database().reference().child(“users/\(userProfile.uid)/M
让您的mapref=Database.Database().reference().child(“映射”)
我试图只访问用户所属的地图。因此,我试着跟随堆栈问题和firebase教程,但我无法掌握如何做到这一点
例如,我想亚当只抓取他的版主地图
让yourMapRef=Database.Database().reference().child(“users/\(userProfile.uid)/Maps”)
我应该如何思考这个问题,如何解决这个问题
用户(根集合结构)
映射(根集合结构)
因此,您要做的是首先获取用户,然后对“地图”集合使用它来检查他们是否调节地图:
func getUsers() {
let userRef = Database.database().reference().child("users").child(currentUserID)
userRef.observeSingleEvent(of: .value, with: { (snapshot) in
let root = snapshot.value as? Dictionary
if let mapsByUser = root["Maps"] as? [String: Bool] {
for (documentId, status) in mapsByUser {
if status {
// Document is true, check for the maps
self.getMaps(key: documentId, owner: currentUserID)
}
}
}
}) { (error) in
print(error.localizedDescription)
}
}
// Check for maps
func getMaps(key:String, owner:String) {
let userRef = Database.database().reference().child("maps").child(key)
userRef.observeSingleEvent(of: .value, with: { (snapshot) in
let user = snapshot.value as? Dictionary
if let mapsByUser = user["mapmoderators"] as? [String: Bool] {
for (userId, status) in mapsByUser {
if userId == owner && status == true {
print("Owner \(owner) manages this \(user)")
}
}
}
}) { (error) in
print(error.localizedDescription)
}
}
在viewDidLoad上调用
getUsers()
以测试此功能。谢谢!我目前正在尝试将答案翻译到我的项目中,看看是否可行。它可能需要编辑以满足您的要求。因为我没有看到详细的结构/etc逐行遍历我的代码。不知道如何开始聊天,否则我会将我的完整代码发送给你。你有Skype吗?我现在有空。Rawandahmad10@outlook.com
{
"-LpY4XEER-b21hwMi9sp" : {
"mapmoderators" : {
"6g55cHXH4begwooHQvO4EKNV3xm1" : true
},
"mapphotoURL" : "https://firebasestorage.googleapis.com/v0/b/...",
"mapusername" : "Hello World"
},
"-LpYo_pQ8zIOGHHlNU1Q" : {
"mapmoderators" : {
"4g99cMTM4begwooORsO4EKNV456" : true
},
"mapphotoURL" : "https://firebasestorage.googleapis.com/v0/...",
"mapusername" : "Dream"
}
}
func getUsers() {
let userRef = Database.database().reference().child("users").child(currentUserID)
userRef.observeSingleEvent(of: .value, with: { (snapshot) in
let root = snapshot.value as? Dictionary
if let mapsByUser = root["Maps"] as? [String: Bool] {
for (documentId, status) in mapsByUser {
if status {
// Document is true, check for the maps
self.getMaps(key: documentId, owner: currentUserID)
}
}
}
}) { (error) in
print(error.localizedDescription)
}
}
// Check for maps
func getMaps(key:String, owner:String) {
let userRef = Database.database().reference().child("maps").child(key)
userRef.observeSingleEvent(of: .value, with: { (snapshot) in
let user = snapshot.value as? Dictionary
if let mapsByUser = user["mapmoderators"] as? [String: Bool] {
for (userId, status) in mapsByUser {
if userId == owner && status == true {
print("Owner \(owner) manages this \(user)")
}
}
}
}) { (error) in
print(error.localizedDescription)
}
}