M1L9 : Reading Data

Lesson 9 from Module 1 of the CWC+ iOS Database course covers reading data back from Firestore.

To read back a single document, we can do this:

let db = Firestore.firestore()
let myCollection = db.collection("collection-name")
let myDocument = myCollection.document("document-name")

document.getDocument { docSnapshot, error in
    if let error = error {
        print(error.localizedDescription)
    } else if let docSnapshot = docSnapshot {
        print(docSnapshot.data()!)
    } else {
        // no data returned
    }
}

Or, we can iterate through all documents, thus:

let db = Firestore.firestore()
let myCollection = db.collection("collection-name")

myCollection.getDocuments { querySnapshot, error in
    if let error = error {
        print(error.localizedDescription)
    } else if let querySnapshot = querySnapshot {
        for doc in querySnapshot.documents {
            print(doc.data())
        }   
    } else {
        // no data returned
    }
}

Seems fairly straightfoward so far.