Slide 58
Slide 58 text
func fetchBook(documentId: String) {
let docRef = Firestore.firestore().collection("books").document(documentId)
docRef.getDocument { document, error in
if let error = error as NSError? {
print("Error getting document: \(error.localizedDescription)")
} else {
if let document = document {
let id = document.documentID
let data = document.data()
let title = data?["title"] as? String
??
""
let numberOfPages = data?["numberOfPages"] as? Int
??
0
let author = data?["author"] as? String
? ?
""
self.book = Book(id:id, title: title,
numberOfPages: numberOfPages, author: author)
}
}
}
}
Fetching a document from Firestore
if let document = document {
let id = document.documentID
let data = document.data()
let title = data?["title"] as? String
??
""
let numberOfPages = data?["numberOfPages"] as? Int
??
0
let author = data?["author"] as? String
? ?
""
self.book = Book(id:id, title: title,
numberOfPages: numberOfPages, author: author)
}
Can we do better?