0
I’m trying to get a document inside a collection, but I can only get it all at once.
The way in the firebase looks like this: Collection: digital-liquors. Documents: adult(That’s what I’m trying to catch), childish, juvenile.
I’m using the following code:
func loadLessons() -> Observable<[LessonsModel]> {
let collection = self.db.collection("licoes-digital")
return Observable.create { observer in
collection.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error: \(err)")
observer.onError(err)
} else {
var items : [LessonsModel] = []
for document in querySnapshot!.documents {
var item = LessonsModel()
let data : [String:Any] = document.data()
let lessons : Array<[String:Any]> = data["licoes"] as! Array
for lesson in lessons {
item.cover = lesson["capa"] as! String
item.content = lesson["conteudo"] as! String
items.append(item)
}
}
observer.onNext(items)
}
}
return Disposables.create()
}
}
I think my mistake is on the line :
collection.getDocuments() { (querySnapshot, err) in
I should replace getDocuments() to get just one document, but I don’t know if there is a specific function for it, or I would have to create it. And if it is necessary to create how to create ?