gpt4 book ai didi

ios - 从 Firebase 检索所有键值并将其附加到数组

转载 作者:行者123 更新时间:2023-11-30 10:55:37 25 4
gpt4 key购买 nike

我需要从 Firebase 中的数据库获取键值并将其附加到数组中。这是我的查询:

let dbHandle = Database.database().reference().child("userRecords").child(uid).queryOrdered(byChild: self.startDate).queryEnding(atValue: self.finishDate)
dbHandle.observeSingleEvent(of: .value, with: { (snapshot) in
guard let foundRecords = snapshot.children.allObjects as? [DataSnapshot] else {return}
for record in foundRecords{
let recordName = record.childSnapshot(forPath: "recordName").value as! String
let recordTime = record.childSnapshot(forPath: "timestamp").value as! String
let recordRate = record.childSnapshot(forPath: "recordRate").value as! String
self.records.append(["equipment": recordName as AnyObject,"date": recordTime as AnyObject, "rate": recordRate as AnyObject])
}

这是我的数据库结构:

Snap (record1) {
"-LUmY5jC-jC6vwpQj26S" = {
recordName = "record1";
recordRate = "1.2";
timestamp = "27/12/2018 at 01:59";
};
"-LUmaAcxLcz1Oif4-L8n" = {
recordName = "record1";
recordRate = "1.0";
timestamp = "25/12/2018 at 02:15";
};}

JSON:

{"userRecords" : {
"l1GIK43GCUbdnnyYsrQnO3JLnjV2" : {
"record1" : {
"-LUmY5jC-jC6vwpQj26S" : {
"recordName" : "record1",
"recordRate" : "1.2",
"timestamp" : "27/12/2018 at 01:59"
},
"-LUmaAcxLcz1Oif4-L8n" : {
"recordName" : "record1",
"recordRate" : "1.0",
"timestamp" : "25/12/2018 at 02:15"
}
},
"record2" : {
"-LUmY7GRwo6_Iq6IqbN_" : {
"recordName" : "record2",
"recordRate" : "1.1",
"timestamp" : "27/12/2018 at 01:59"
},
"-LUmaAcr1P2mQrtSdFWi" : {
"recordName" : "record2",
"recordRate" : "1.2",
"timestamp" : "25/12/2018 at 02:15"
}
}
}

},}

查询抛出“线程 1:信号 SIGABRT”。所有对象均已正确连接。当我将查询更改为:

 dbHandle.observeSingleEvent(of: .childAdded, with: { (snapshot) in

查询工作正常。

结果会附加到我的数组“记录”中,然后存储到字典中返回 Firebase:

let reportsRef = Database.database().reference().child("reports").child(uid)
let reportDict = ["reportNumber": reportNumber, "company": self.companyTxtField.text!, "address": self.addressTxtField.text!, "start": self.startDate, "finish": self.finishDate, "records": self.records] as [String : Any]
reportsRef.childByAutoId().setValue(reportDict)

问题:

  1. 我仅从“record1”获取附加到数组的值虽然至少还有“record2”。如何从“foundRecords”中的所有对象中获取所有值并将这些值附加到数组 records: [[String: AnyObject]] 中?
  2. 为什么“observeSingleEvent(of: .value”会抛出错误?

*出于演示目的,时间戳值尚未转换为 NSDate 格式

最佳答案

每个 UID 下的 JSON 中有一个您的代码无法处理的额外级别。当您观察 .childAdded 时,这不是问题,因为您的完成处理程序会为每个单独的子项调用。但是当您观察 .value 时,系统会调用您并提供所有子项的快照。这意味着您必须自己循环额外的级别:

let dbHandle = Database.database().reference().child("userRecords").child(uid)
.queryOrdered(byChild: "timestamp").queryStarting(atValue: self.startDate).queryEnding(atValue: self.finishDate)
dbHandle.observeSingleEvent(of: .value, with: { (snapshot) in
guard let foundRecords = snapshot.children.allObjects as? [DataSnapshot] else {return}
for record in foundRecords {
let recordName = record.childSnapshot(forPath: "recordName").value as! String
let recordTime = record.childSnapshot(forPath: "timestamp").value as! String
let recordRate = record.childSnapshot(forPath: "recordRate").value as! String
self.records.append(["equipment": recordName as AnyObject,"date": recordTime as AnyObject, "rate": recordRate as AnyObject])
}
}

关于ios - 从 Firebase 检索所有键值并将其附加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53979995/

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