访问childByAutoId Firebase Swift

时间:2017-03-20 06:19:14

标签: ios swift firebase firebase-realtime-database

我想找到具有特定用户名的每个自动ID,并更改该特定AutoID中的值。

每个具有我要查找的用户名的自动ID。然后按字符串更改与我指定的用户名匹配的所有自动ID上的特定值。

所以每个AutoID都有用户名:" s7iytsdifytgsfyg" (实施例)

然后更改所有这些Auto ids photo_url值。

enter image description here

enter image description here

//configureAuth()
        NotificationCenter.default.addObserver(self, selector: #selector(updateValue(_ :)), name: Notification.Name("TIME_TO_UPDATE"), object: nil)


func updateValue(_ notification: Notification) {

        //Firebase Initialization
        var ref: FIRDatabaseReference!
        //var storage: FIRStorageReference!
        let userID = FIRAuth.auth()?.currentUser?.uid
        ref = FIRDatabase.database().reference()
        //storage = FIRStorage.storage().reference()


        //Get Data from database resend to database
        ref.child("Users").child(userID!).observeSingleEvent(of: .value, with: {(snapshot) in
            let snapDict = snapshot.value as? NSDictionary
            let firebaseUserPhotoURL = snapDict?["photo_url"] as? String ?? ""

        for key in self.keys {
            self.ref.child("general_room").child(key).updateChildValues(["photo_rul": firebaseUserPhotoURL])
        }

        })
    }

    func changeChatPhoto() {

        //Firebase Initialization
        var ref: FIRDatabaseReference!
        let userID = FIRAuth.auth()?.currentUser?.uid
        ref = FIRDatabase.database().reference()

        let observer = ref.child("general_room").observe(.value, with: { (snapshot) in
            let messages = snapshot.value as? [String: Dictionary<String, String>]
            for (key, value) in messages! {
                if value["user_name"] == userID { // Field you want to find
                    self.keys.append(key) // keep track of the key (auto id)
                }
            }
            NotificationCenter.default.post(name: Notification.Name("TIME_TO_UPDATE"), object: nil)
        })

        // Just observe for one time
        ref.child("general_room").removeObserver(withHandle: observer)


    }

1 个答案:

答案 0 :(得分:2)

很简单,您只需找到包含username的所有密钥,然后更新其值。

ref.child("general_room").observeSingleEvent(of: .value, with: { (snapshot) in
        let general_room = snapshot.value as! [String: Dictionary<String, String>]
        for (key, value) in general_room {
            if value["user_name"] == "HELLO WORLD" { // Field you want to find
                self.keys.append(key) // keep track of the key (auto id)
            }
        }
        NotificationCenter.default.post(name: Notification.Name("TIME_TO_UPDATE"), object: nil)
    })

我也在viewDidLoad中注册Notification,它告诉我从firebase检索所有数据。

var keys = [String]()
override func viewDidLoad() {
    ...
    NotificationCenter.default.addObserver(self, selector: #selector(updateValue(_ :)), name: Notification.Name("TIME_TO_UPDATE"), object: nil)
}

func updateValue(_ notification: Notification) {
    for key in keys {
       self.ref.child("general_room").child(key).updateChildValues(["photo_rul": "http://google.com"])
    }
}