如何在不知道名称的情况下访问Firestore字段值?

时间:2019-05-05 17:20:37

标签: swift firebase google-cloud-firestore

我正在像这样将数据上传到Firestore:

 func uploadTrackedSymptomValues(symptom: String, comment: String, time: String, timestamp: Int) {
    print("Uploading symptom values.")
    let user_id = FirebaseManager.shared.user_id
    let docRef = db.collection("users").document(user_id!).collection("symptom_data").document("\(symptom)_data")

    let updateData = [String(timestamp) : ["symptom" : symptom, "severity" : severity, "comment" : comment, "timestamp" : String(timestamp)]]
    docRef.setData(updateData, merge: true)
    docRef.setData(updateData, merge: true) { (err) in
        if err != nil {
            print(err?.localizedDescription as Any)
            self.view.makeToast(err?.localizedDescription as! String)
        } else {
            print("Symptom Data Uploaded")
            self.view.makeToast("\(symptom) logged at \(time). Severity: \(self.severity). Comment: \(comment)", duration: 2.0, position: .center, title: "Success!", image: self.cellImage) { didTap in
                if didTap {
                    print("completion from tap")
                } else {
                    print("completion without tap")
                }
            }
        }
    }
}

在Firestore上,它看起来像:enter image description here

现在我想使用时间戳记下的每个字段作为UITableViewCell,但是我不确定如何访问文档下的字段,例如1556998898下的Anxiety_data,所以我可以访问:

 comment = "comment";
 severity = "Mild";
 symptom = "Anxiety";
 timestamp = 1556998898;

要在UITableViewCell中使用,对于实时数据库,我将使用childAdded listener。我尝试过:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let index = indexPath.row
    let cell = tableView.dequeueReusableCell(withIdentifier: "EntryDataCell", for: indexPath) as! EntryDataCell
    cell.configureCustomCell()

    let user_id = FirebaseManager.shared.user_id
    let symptomRef = db.collection("users").document(user_id!).collection("symptom_data").document(symptomSections[index])
    symptomRef.getDocument(completion: { (document, err) in
        if let document = document, document.exists {
            let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
            print("\(document.documentID) : \(dataDescription)")
            cell.commentLabel.text = dataDescription //<- Stuck here

        } else {
            print("Document does not exist")
        }
    })

    return cell
}

但是我对于如何获得特定字段还是很困惑的,因为我不知道名字,因为它是制作时间的时间戳,而不是整个文档的时间戳。如果有更好的解决方法,请告诉我。谢谢。

编辑:

我能够获取键(时间戳)和值,但是在解析值时遇到了麻烦:

 let obj = dataDescription
            for (key, value) in obj! {
                print("Property: \"\(key as String)\"") //<- prints Timestamp
                print("Value: \"\(value)\"") // <- prints the comment,severity,symptom,timestamp fields I need.
            }

如果我尝试cell.titleLabel.text = value["comment"],我会得到Value of type 'Any' has no subscripts

我考虑过使用struct:

   struct FieldValues: Codable {
let comment: String
let severity: String
let symptom: String
let timestamp: Int
 }

但是不确定如何在value中使用它。对于最后一部分,我感到有些烦恼。

0 个答案:

没有答案