I've began writing a Notification/Events app for my School as a Project. Im very new to Swift and Firebase. I used Storybaord.
I have a function that gets the Amount of Events/Posts in the Database:
func getPostCount() -> Int {
var count = 0
messageDB.collection("MainPosts").getDocuments()
{
(querySnapshot, err) in
if let err = err
{
print("Error getting documents: \(err)");
}
else
{
for document in querySnapshot!.documents {
count += 1
}
print(count) // => should be 10 is 10
}
print(count) // => should be 10 is 10
}
print(count) // => should be 10 is 0
return count
}
I'm getting all Documents from the DB and foreach I add 1 to the count. But when i try to return it after the
messageDB.collection("MainPosts").getDocuments(){}
the value is 0. If Im going to try returning the value inside of the Firestore operation I going to get the error:
Unexpected non-void return value in void function
What should i do?
Console:
0
10
10
I think i know why but Im not sure.
Is the function:
messageDB.collection("MainPosts").getDocuments(){}
Async? And if so is there a await keyword or something similar?