title: Why does my function return the wrong parameter preliminarily?
body: <p>I currently have a button that when pressed checks if a function returns a true or false parameter. The function in question checks a code in a database and returns true if it is there, and false if it is not. However, the function returns false by default as it takes a second for the database to return a response. Is there any way for me to get the function to wait for the response to come in before returning a response to the if statement in question? That way when the button is pressed and the if statement runs which checks if the function result is true or false, it gets an accurate response? Enclosed is the if statement itself, as well as the function.</p>
  <pre><code> if ps.getNEWStatus(name: partnerCodeField.text!) == true {
                  partnerCode = partnerCodeField.text!
                  ps.getStatus()
                  
              } else if ps.getNEWStatus(name: partnerCodeField.text!) == false {
                  holdText.text = &quot;Partner code is invalid.&quot;
                  
              }
  </code></pre>
  <p>Function</p>
  <pre><code>     func getNEWStatus(name: String) -&gt; Bool {
          
          db.collection(&quot;users&quot;).document(name)
  .addSnapshotListener { documentSnapshot, error in
                guard let document = documentSnapshot else {
                //  print(&quot;Error fetching partner status: \(error!)&quot;)
                  return
                }
                guard let data = document.data() else {
                //  print(&quot;No partner with this code exists.&quot;)
                  isPartnerValid = false
                  return
                }
                  if let isOnline = data[&quot;isOnline&quot;] as? Int {
                      isPartnerValid = true
                      
                  }
              }
          
          if isPartnerValid == true {
          return true
          } else if isPartnerValid == false {
              return false
          }
          return false
      }
  </code></pre>