I am building an IOS app and I am at the stage where I want to load some categories in a pickerview. The list of categories is stored in Firestore as documents.
I have a function that does the following:
- Initiate an array
- Read data from Firestore
- Append the array with values
- Return the array
The problem is that the function seems to return the array without even getting any results from Firestore
My data model
class categoryModel {
var catImg=""
var catName=""
var catID=""
//set categories
init(catImg: String, catName:String, catID:String){
self.catName=catName
self.catImg=catImg
self.catID=catID
}
}
Firestore reference
struct FirestoreReferenceManager {
static let event = "events"
static let db = Firestore.firestore()
static let rootEvents = db.collection("events")
static let rootUsers = db.collection("users")
}
Firestore service
class FirestoreService {
static func getCategoryfromDB()->[categoryModel] {
let rootCategory = FirestoreReferenceManager.rootEvents
var tabCategory=[categoryModel]()
rootCategory.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
tabCategory.append(categoryModel(catImg: document.data()["img"] as! String, catName:document.data()["name"] as! String, catID: document.documentID))
print("number of categories running: \(tabCategory.count)")
}
}
}
print("number of categories final: \(tabCategory.count)")
return tabCategory
}
}
Check what the log is showing
umber of categories final: 0
number of categories final: 0
number of categories running: 1
number of categories running: 2
number of categories running: 3
number of categories running: 4
number of categories running: 5
number of categories running: 6
number of categories running: 7
number of categories running: 8
number of categories running: 9
number of categories running: 10
number of categories running: 11
number of categories running: 1
number of categories running: 2
number of categories running: 3
number of categories running: 4
number of categories running: 5
number of categories running: 6
number of categories running: 7
number of categories running: 8
number of categories running: 9
number of categories running: 10
number of categories running: 11
The 2 first lines should actually be the lasts.
And when I read Firestore from a viewcontroller, the array is empty. I tried hardcoding the data model and it is working fine.
I read about some "delay" in getting data, but then how to fix this?
How do I solve the problem?