//Para leer datos de la base de datos, podemos utilizar los siguientes métodos: // Leer todos los documentos de una colección db.collection("cities").getDocuments() { (querySnapshot, error) in if let error = error { print("Error al obtener documentos: \(error.localizedDescription)") return } for document in querySnapshot!.documents { let data = document.data() let name = data["name"] as? String ?? "" let population = data["population"] as? Int ?? 0 print("Ciudad: \(name), Población: \(population)") } } // Leer un documento específico de una colección db.collection("cities").document("SF").getDocument() { (document, error) in if let document = document, document.exists { let data = document.data() let name = data?["name"] as? String ?? "" let population = data?["population"] as? Int ?? 0 print("Ciudad: \(name), Población: \(population)") } else { print("El documento no existe") } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter