从Firebase检索所有键值并追加到数组

时间:2018-12-30 17:48:06

标签: ios swift firebase firebase-realtime-database

我需要从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

查询工作正常。

结果被添加到我的数组“ records”中,然后存储到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格式

1 个答案:

答案 0 :(得分:0)

在您的代码无法处理的每个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])
  }
}