简体   繁体   中英

How to confirm the data from Firestore is appended in swift?

I am using the below code to append the data from Firestore in Swift IOS, but when I use "print" function to check whether the data is retrieved or not it doesn't print any actual data all it prints is the the below kind of details, How should I confirm the data is appended accurately?

CODE

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)

Print Result

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>])

As you can see in the output, the documents as FIRQueryDocumentSnapshot objects , which contain both the data and some metadata about the results.

To get the data from the snapshot, call its 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)
}

Note that this is also quite well described in the Firebase documentation on reading all documents from a collection , so I recommend spending some time studying that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM