gpt4 book ai didi

ios - 无法更新 childAutoId 节点中的节点值以更新字典值

转载 作者:可可西里 更新时间:2023-11-01 01:49:05 24 4
gpt4 key购买 nike

当我向 firebase 添加新数据时,我正在开发体重跟踪应用程序,它工作正常,但是当我从该日期更新数据时,新数据正在添加现有电子邮件的唯一 ID 我想更新当前日期重量,如果我在那一天再次增加体重[黑色圆圈显示我要更新权重的位置,其余数据是实际数据正在更新的位置

这也是我的代码。

@IBAction func addButton(_ sender: UIButton) {

let time = Date()
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.yyyy"
let formatteddate = formatter.string(from: time as Date)
if formatteddate == addDate.text!{
self.UpdateTaskWithName(name: addWeight.text!, date: addDate.text!) { (success) in
print("**** TASK UPDATED *******")
DispatchQueue.main.async {
self.navigationController?.popViewController(animated: true)

}
}

}else{

if let text = self.addWeight.text{
if let date = self.addDate.text{

self.saveTaskWithName(name: text, date: date, completionHandler: { (success) in
print("**** NEW TASK ADDED *******")
DispatchQueue.main.async {
self.navigationController?.popViewController(animated: true)
}
})
}
}

}

}

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").child(userid).childByAutoId().setValue(dict) { (error, reference) in

if let err = error{
print(err.localizedDescription)
}else{
// data save successfully
completionHandler(true)
}
}
}
}



func UpdateTaskWithName(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").child(userid).updateChildValues(dict) { (error, reference) in

if let err = error{
print(err.localizedDescription)
}else{
// data save successfully
completionHandler(true)

}
}
}
}

screenshot

最佳答案

在这个示例中,您可以执行查询 来查找您的日期与您感兴趣的日期匹配的节点,如果找到则更新。该查询将向您返回节点的 key(对于 childByAutoId 节点),您可以使用该节点更新子 weight:

func UpdateTaskWithName(name:String, date:String, completionHandler:@escaping ((_ success:Bool)->Void)){
if let userid = Auth.auth().currentUser?.uid {
self.ref.child("WeightTracker")
.child(userid)
.queryOrdered(byChild: "date")
.queryEqual(toValue: date)
.observeSingleEvent(of: .value, with: { snapshot in
self.ref.child("WeightTracker").child(userid).child(snapshot.key).child("weight").setValue(name)
})
}
}

考虑到 name 您的意思是您的 date 来自 UI TextField 并考虑在内,这就是您的函数的样子。

通过将一些操作拆分为变量实例并像这样重新使用它们,还可以使该函数更易于阅读:

func UpdateTaskWithName(name:String, date:String, completionHandler:@escaping ((_ success:Bool)->Void)){
if let userID = Auth.auth().currentUser?.uid {
let userIDRef = self.ref.child("WeightTracker").child(userID)
let query = userIDRef.queryOrdered(byChild: "date").queryEqual(toValue: date)
query.observeSingleEvent(of: .value, with: { snapshot in
let childByAutoIDNodeKey = snapshot.key
let refToUpdate = userIDRef.child(childByAutoIDNodeKey).child("weight")
refToUpdate.setValue(name)
completionHandler(true)
})
}
}

关于ios - 无法更新 childAutoId 节点中的节点值以更新字典值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56256736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com