FIRQueryDocumentSnapshot

FIRQueryDocumentSnapshot

我正在使用以下代码在Swift IOS中从Firestore追加数据,但是当我使用“打印”功能检查是否检索到数据时,它不会打印任何实际数据
它打印的是以下类型的详细信息,如何确认数据正确附加?



var messages: [DocumentSnapshot]! = []

 ref.addSnapshotListener { querySnapshot, error in
             guard let documents = querySnapshot?.documents else {
                 print("Error fetching documents: \(error!)")
                 return
             }

            for doc in documents {
              self.messages.append(doc)
              self.clientTable.insertRows(at: [IndexPath(row: self.messages.count-1, section: 0)], with: .automatic)
                //self.clientTable.reloadData()

            }

print(messages)


打印结果


  Optional([<FIRQueryDocumentSnapshot: 0x60000123cbe0>, <FIRQueryDocumentSnapshot: 0x60000123ccd0>, <FIRQueryDocumentSnapshot: 0x60000123cd20>, <FIRQueryDocumentSnapshot: 0x60000123cd70>, <FIRQueryDocumentSnapshot: 0x60000123cdc0>, <FIRQueryDocumentSnapshot: 0x60000123ce10>, <FIRQueryDocumentSnapshot: 0x60000123ce60>, <FIRQueryDocumentSnapshot: 0x60000123cf00>, <FIRQueryDocumentSnapshot: 0x60000123cf50>, <FIRQueryDocumentSnapshot: 0x60000123cfa0>, <FIRQueryDocumentSnapshot: 0x60000123cff0>, <FIRQueryDocumentSnapshot: 0x60000123ceb0>, <FIRQueryDocumentSnapshot: 0x60000123d040>, <FIRQueryDocumentSnapshot: 0x60000123d090>, <FIRQueryDocumentSnapshot: 0x60000123d0e0>, <FIRQueryDocumentSnapshot: 0x60000123d130>, <FIRQueryDocumentSnapshot: 0x60000123d180>, <FIRQueryDocumentSnapshot: 0x60000123d1d0>, <FIRQueryDocumentSnapshot: 0x60000123d220>, <FIRQueryDocumentSnapshot: 0x60000123d270>, <FIRQueryDocumentSnapshot: 0x60000123d2c0>, <FIRQueryDocumentSnapshot: 0x60000123d310>, <FIRQueryDocumentSnapshot: 0x60000123d360>, <FIRQueryDocumentSnapshot: 0x60000123d3b0>, <FIRQueryDocumentSnapshot: 0x60000123d400>, <FIRQueryDocumentSnapshot: 0x60000123d450>, <FIRQueryDocumentSnapshot: 0x60000123d4a0>])

最佳答案

正如您在输出中看到的,documentsFIRQueryDocumentSnapshot objects,其中包含数据和有关结果的一些元数据。

要从快照获取数据,请调用其FIRQueryDocumentSnapshot.data method

var messages: [DocumentSnapshot]! = []

ref.addSnapshotListener { querySnapshot, error in

     guard let documents = querySnapshot?.documents else {
         print("Error fetching documents: \(error!)")
         return
     }

    for doc in documents {
      self.messages.append(doc.data())
    }

    print(messages)
}


请注意,这在reading all documents from a collection上的Firebase文档中也有很好的描述,因此我建议花一些时间研究它。

关于ios - 如何确认Firestore中的数据是否 swift 添加?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58913061/

10-12 01:19