0

I have this swift code:

private func checkPhoneNumber(number: String) -> Bool {
       var ret = true
       self.db.collection("customers").whereField("telephone", isEqualTo: number).getDocuments(){ (querysnapshot,err) in
               if querysnapshot!.documents.count>0{
                   self.err="This Phone number is already in use"
                   ret=false
               }
       }
       return ret
   }

It sends a request to firebase to check if the phone number is in use. The return value is based on the getDocuments() function. However, getDocuments() takes 1-2 seconds to complete, heence checkPhoneNumber always returns true, before the nested function has time to change the ret value. I can't do the return ret inside of getDocuments(), because it's a different void function. How can I make checkPhoneNumber wait till the function inside is completed and then return a value?

  • The problem is that `getDocuments()` is an asynchronous function, not simply a nested function call, so you cannot just use the synchronous `return` keyword. – Dávid Pásztor Jun 15 '20 at 09:14
  • @DávidPásztor i put the reqeust in ```DispatchQueue.global(qos: .userInitiated).async```, but nothing changed – Murad Shahmammadli Jun 15 '20 at 09:26
  • As explained in the linked duplicate target, you need to use a completion handler. `DispatchQueue.async` has nothing to do with your problem or the solution for it. – Dávid Pásztor Jun 15 '20 at 09:32

0 Answers0