I wrote this code to retrieve the current user name, it gets the name right but it keep looping, what can I do to solve it?
as you can see I put a print statement and its all loop. I hope my problem description is clear and get a solution.
func getChildName1 ()->String
{
let db = Firestore.firestore()
var childName : String = "nil"
db.collection("Child").getDocuments { snapshot, error in
if error == nil {
if let snapshot = snapshot {
print("in childName snapshot") //this keeps printing
DispatchQueue.main.async {
print("in childName DispatchQueue.main.async") //this keeps printing
self.childList = snapshot.documents.map { d in
Child(
id: d.documentID,
email:d["Email"]as? String ?? "",
name: d["name"]as? String ?? ""
)
}
}
}
}
else {
// Handle the error
}
}
for item in childList
{
if item.id == String(Auth.auth().currentUser!.uid)
{
childName = item.name
print(childName) //this keeps printing
}
}
return childName
} // end getChildName