尝试将数据添加到Firebase
,但是数据没有添加到Firebase
上,这是我的代码?
@IBAction func addButton(_ sender: UIButton) {
if let text = self.addWeight.text{
if let date = self.addDate.text{
self.saveTaskWithName(name: text, date: date, completionHandler: { (success) in
// data save successfully
print("**** TASK ADDED *******")
})
}
}
}
func saveTaskWithName(name:String,date:String, completionHandler:@escaping ((_ success:Bool)->Void)){
let dict = ["weight": name, "date" : date] as [String : Any]
if let userid = Auth.auth().currentUser?.uid{
self.ref.child("WeightTracker/\(userid)").childByAutoId().setValue(dict) { (error, reference) in
if let err = error{
print(err.localizedDescription)
}else{
// data save successfully
completionHandler(true)
}
}
}
}
答案 0 :(得分:1)
请尝试以下解决方案
func saveTaskWithName(name:String,date:String, completionHandler:@escaping ((_ success:Bool)->Void)){
let dataBaseRef = Database.database().reference()
let dict = ["weight": name, "date" : date] as [String : Any]
if let userid = Auth.auth().currentUser?.uid{
dataBaseRef.child("WeightTracker").child(userid).setValue(dict) { (error, reference) in
if let err = error{
print(err.localizedDescription)
}else{
// data save successfully
completionHandler(true)
}
}
}
}