更新Firebase中的值

时间:2017-07-24 19:02:12

标签: ios firebase swift3 firebase-realtime-database

func updateFirebase(){
    myFun = thisIsMyFunTextView.text
    IAm = iAmTextView.text
    var profileKey = String()
    profileRef.queryOrdered(byChild: "uid").queryEqual(toValue: userID).observe(.value, with:{
        snapshot in
            for item in snapshot.children {
                guard let data = item as? FIRDataSnapshot else { continue }
                guard let dict = data.value as? [String: Any] else { continue }
                guard let profileKey = dict["profileKey"] else { continue }
                self.profileRef.child(profileKey as! String).child("bodyOfIAM").setValue(IAm)
                self.profileRef.child(profileKey as! String).child("bodyOfThisIsMyFun").setValue(myFun)
        }
    })
}
@IBAction func backButtonClicked(_ sender: Any) {
    updateFirebase()
    DispatchQueue.main.asyncAfter(deadline: .now() + 4, execute: {
        self.dismiss(animated: true)
    })
}

myFun和IAm由用户对textview的更改成功定义。我无法提取childByAutoID值而不触发此循环,该循环不会在调用后结束,即使在显示新的视图控制器时也会继续。在此循环期间,“bodyOfThisIsMyFun”在旧值和新值之间摇摆,而“bodyOfIAM”立即被正确地重新定义并保持原样。如何获取提取的新值以替换旧值?

1 个答案:

答案 0 :(得分:0)

我需要在for ... in语句的末尾添加这行代码: self.profileRef.removeAllObservers()