在我的Firebase数据库中,父数据是所有用户的userId
。他们每个人都有一个child("friends")
,其中包含他们所有朋友的userId
的列表。
我想获得每个currentUser
的朋友userId
并将currentUser
的{{1}}添加到他们每个朋友的userId
节点
这是我的数据结构的样子:
child("closeFriends")
我希望'closeFriends'是具有users
10XeC8j6OCcFTKj8Dm7lR7dVnkf1
email: "Dummy19@gmail.com"
fcmToken: "cBW9XhpYqio:APA91bF6K5YgRj0PgyDCoApNpIWGO4icwFg..."
friends
ekFjYJU8OWTbYnQXVzhcxVE0wBP2: true
profileImageUrl: "https://firebasestorage.googleapis.com/v0/b/jar..."
username: "Dummy19"
ePt3MSOuk6ZYDfekewmPlG4LgcU2
email: "Dummy20@gmail.com"
fcmToken: "cBW9XhpYqio:APA91bF6K5YgRj0PgyDCoApNpIWGO4icwFg..."
friends
ekFjYJU8OWTbYnQXVzhcxVE0wBP2: true
u43NtDYmqeTxafmLYjxxt60ngUo1: true
profileImageUrl: "https://firebasestorage.googleapis.com/v0/b/jar..."
username: "Dummy20"
ekFjYJU8OWTbYnQXVzhcxVE0wBP2
email: "Dummy18@gmail.com"
fcmToken: "cF-b_Fd8Ufc:APA91bG9bir1o_jdJdfB35l9g9HlNNTNHPM..."
friends
10XeC8j6OCcFTKj8Dm7lR7dVnkf1: true
ePt3MSOuk6ZYDfekewmPlG4LgcU2: true
profileImageUrl: "https://firebasestorage.googleapis.com/v0/b/jar..."
username: "Dummy18"
u43NtDYmqeTxafmLYjxxt60ngUo1
email: "Dummy21@gmail.com"
fcmToken: "cBW9XhpYqio:APA91bF6K5YgRj0PgyDCoApNpIWGO4icwFg..."
friends
ePt3MSOuk6ZYDfekewmPlG4LgcU2: true
profileImageUrl: "https://firebasestorage.googleapis.com/v0/b/jar..."
username: "Dummy21"
的节点,就像userId
一样。
这些是我的参考变量和函数:
friends
我尝试了这个let userReference = Database.database().reference().child("users")
var currentUserReference: DatabaseReference {
let id = Auth.auth().currentUser!.uid
return userReference.child("\(id)")
}
var currentUserFriendsReference: DatabaseReference {
return currentUserReference.child("friends")
}
var currentUserCloseFriendsReference: DatabaseReference {
return currentUserReference.child("closeFriends")
}
var currentUserId: String {
let uid = Auth.auth().currentUser!.uid
return uid
}
func getCurrentUser(_ completion: @escaping (User) -> Void) {
currentUserReference.observeSingleEvent(of: DataEventType.value) { (snapshot) in
let email: String = snapshot.childSnapshot(forPath: "email").value as! String
let uid = snapshot.key
let username = snapshot.childSnapshot(forPath: "username").value as! String
let profileImageUrl = snapshot.childSnapshot(forPath: "profileImageUrl").value as! String
completion(User(uid: uid, userUsername: username, userProfileImageUrl: profileImageUrl, userEmail: email))
}
}
func getUser(_ userId: String, completion: @escaping (User) -> Void) {
userReference.child(userId).observeSingleEvent(of: DataEventType.value, with: { (snapshot) in
let email = snapshot.childSnapshot(forPath: "email").value as! String
let uid = snapshot.key
// Did the same thing here as in the above function
let username = snapshot.childSnapshot(forPath: "username").value as! String
let profileImageUrl = snapshot.childSnapshot(forPath: "profileImageUrl").value as! String
completion(User(uid: uid, userUsername: username, userProfileImageUrl: profileImageUrl, userEmail: email))
})
}
var closeFriendList = [User]()
循环,但没有执行任何操作:
for
作为参考,func addCurrentUserToCloseFriendsLists(_ userId: String){
for friend in friendList{
let id = friend.uid
self.getUser(id) { (user) in
self.closeFriendList.append(user)
self.userReference.child(userId).child("closeFriends").child(self.currentUserId).setValue(true)
}
}
是在另一个文件(我的“ FriendSystem”)中创建的朋友的列表。
我确定还有另一种方法来获取所有用户的朋友,然后运行循环以将friendList
添加到所有这些朋友的currentUser
中,我只是无法弄清楚
超级固守于此,希望您能为您提供帮助!谢谢!
答案 0 :(得分:1)
我想获取每个currentUser的朋友的userId并添加 currentUser的每个朋友子级的userId(“ closeFriends”) 节点
让我们从一个与您相似的简单用户结构开始:
users
uid_0
name: "Larry"
friends:
uid_1: true
uid_2: true
uid_1
name: "Moe"
uid_2
name: "Curly"
这个想法是浏览这个用户(当前用户)的朋友列表,并将这个用户的uid添加到其他用户的朋友列表中。在此示例中,我只是为每个用户创建一个“朋友”节点,但您可以将其设为“ closeFriends”或您想要的任何名称。
这是执行此操作的代码
let thisUserId = "uid_0"
func addThisUserIdToFriendsNode() {
let usersRef = self.ref.child("users") //the general users node
let thisUserRef = usersRef.child(self.thisUserId) //this users node
let thisUserFriends = thisUserRef.child("friends") //this users list of friends
//load in all of this users friends
thisUserFriends.observeSingleEvent(of: .value, with: { snapshot in
let allOfThisUsersFriends = snapshot.children.allObjects as! [DataSnapshot]
for user in allOfThisUsersFriends {
let friendRef = usersRef.child(user.key) //the friend ref
let friendsFriendRef = friendRef.child("friends") //a friend friends child node
let nodeToAdd = friendsFriendRef.child(self.thisUserId) //add this uid
nodeToAdd.setValue(true)
}
})
}
和结果
users
uid_0
name: "Larry"
friends:
uid_1: true
uid_2: true
uid_1
name: "Moe"
friends
uid_0: true
uid_2
name: "Curly"
friends
uid_0: true
答案 1 :(得分:0)
我想,如果我对您的问题理解正确,那么您可以尝试使用Cloud DataStore代替RealtimeDB。 您可以在here中找到此数据库之间的差异描述。
另一种方法-使用Firebase函数。