Firebase Swift-UpdateChildValues()函数失败

时间:2019-02-20 02:48:41

标签: ios swift firebase firebase-realtime-database

我正在努力在代码底部获取print(followingValues)的值。我只是在控制台中得到了这样的结果:

[“ tz5RNZkKMmQwCJbSmwir2LNLrpg2”:[:]]

我有点困惑,因为较早的print(followingUserData)可以正确地与userData一起打印,但是在底部,好像无法访问followingUserData。有人可以帮助我,以便print(followingValues)起作用并随后使用随后的updateChildValues函数更新Firebase数据库。我觉得我已经看了很久了,这是一个非常明显的答案。

@objc func handleEditProfileOrFollow()    {

    guard let currentUserId = Auth.auth().currentUser?.uid else { return }
    guard let selectedUserId = user?.uid else { return }
    var followingUserData = [String: Any]()
    Database.database().reference().child("users").child(selectedUserId).observeSingleEvent(of: .value) { (snapshot) in
        guard let dictionary = snapshot.value as? NSDictionary else { return }
        let username = dictionary["username"] as? String
        let image = dictionary["image_url"] as? String
        followingUserData = [
            "username" : username,
            "image_url" : image
        ]
        print(followingUserData)
    }


    if editProfileFollowButton.titleLabel?.text == "Unfollow" {
        // unfollow
        Database.database().reference().child("users").child(currentUserId).child("following").child(selectedUserId).removeValue { (err, ref) in
            if let err = err    {
                print("Failed to unfollow user:", err)
                return
            }
            self.setupFollowStyle()
            }
            Database.database().reference().child("users").child(selectedUserId).child("followers").child(currentUserId).removeValue { (err, ref) in
            if let err = err    {
                print("Failed to unfollow user:", err)
                return
            }
            self.setupFollowStyle()
        }

    }   else    {
        // following
        let followingRef = Database.database().reference().child("users").child(currentUserId).child("following")
        let followingValues = [selectedUserId: followingUserData]
        print(followingValues)
        followingRef.updateChildValues(followingValues) { (err, ref) in
            if let err = err    {
                print("failed to add to following:", err)
                return
            }

0 个答案:

没有答案