在我的Firebase数据库中,父数据是所有用户的userIds,每个用户都有一个child("friends")列表,其中包含他们所有朋友的userIds列表。
我想让每个currentUser's friends'userIds并将currentUser'suserId添加到其每个朋友的child("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"

我希望'closeFriends'成为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]()

我试过这个循环,但什么也没做:
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)
        }
    }

作为参考,for是在另一个文件(我的“FriendSystem”)中创建的朋友列表。
我确信有另一种方法可以获取所有用户的朋友,然后运行一个循环将friendList添加到所有那些朋友的currentUser中,我只是还没有弄清楚。
超级粘在这上面,希望你能帮忙!谢谢!

最佳答案

我想获取每个当前用户朋友的用户ID并添加
当前用户对其朋友的每个孩子的用户名(“closeFriends”)
节点
让我们从一个与您类似的简单用户结构开始:

users
   uid_0
      name: "Larry"
      friends:
         uid_1: true
         uid_2: true
   uid_1
       name: "Moe"
   uid_2
       name: "Curly"

我们的想法是浏览这个用户(当前用户)的好友列表,并将这个用户uid添加到其他用户的好友列表中。在这个例子中,我只是为每个用户创建了一个“friends”节点,但是您可以将其设置为“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

10-05 21:01
查看更多