I am wanting to use Firestore to retrieve user info and other data linked to that user once they have logged in via firebase auth. On the home page of the app I use .onAppear{ pulluserData() }. I understand that Firestore functions are asynchronous so how can I wait for this data to be pulled before displaying it to the user on the home screen?
Here is my function to check the database:
func checkDatabase() async { //Function that will check the database. Will be good to add a listener eventually if self.pullUserData{ await dbm.readUser(userID: "VSWAq7QCw3dbGYwMdtClbbANGVe2") } } and the actual database function:
func readUser(userID: String)async{ //Function that will be used to read user info from the database let userRef = database.collection("users") do{ let doc = try await userRef.document(userID).getDocument().data() print("The doc is: ") print(doc as Any) } catch { } }
Taskand update a variable with the result. It can be an@Publishedand your view will refresh when it has the new value.printstatements are within the example is where you would populate the UI. The above comment doesn't appear to be correct - firebase network calls run on background threads so no UI issues.