title: Ensuring Async Action Has Completed in Firebase
body: <p>I need to run concurrent queries to Firebase in swift. How can I ensure that a looped query has finished before another action is allowed to start in my app?</p>
  
  <p>For example, the first query is a straightforward, and simply pulls data. but the second, iterates through an array looking for a certain node in my firebase database, looping through the array <code>self.contacts</code>:</p>
  
  <pre><code> // First check if friend, and remove/add to
              friendsURL.observeSingleEventOfType(.Value, withBlock: { snapshot in
  
                  for oneSnapshot in snapshot.children {
  
                      for oneContact in contactsArray {
  
                          for oneContactPhoneNum in oneContact.phoneNumbers {
  
                              let phoneNumber = oneContactPhoneNum.value as! CNPhoneNumber
  
                              contactNumber = phoneNumber.stringValue
  
                              // Clean the number
                              let stringArray = contactNumber!.componentsSeparatedByCharactersInSet(
                                  NSCharacterSet.decimalDigitCharacterSet().invertedSet)
                              let newString = "1" + stringArray.joinWithSeparator("")
  
                              let firebaseFriendNumber = oneSnapshot.value["phoneNumber"] as! String
  
                              if newString == firebaseFriendNumber {
  
                                  self.friends.append(Friend(userName: oneSnapshot.value["userName"] as! String,phoneNumber: firebaseFriendNumber, status: 2, name: oneContact.givenName, userID: oneSnapshot.key))
  
                                  // Remove that contact
                                  self.contacts.removeObject(oneContact)
                              }
                          }
                      }
                  }
  
                  // Now do the users search:
                  for oneContact in self.contacts {
                      for oneContactNumer in oneContact.phoneNumbers {
  
                          let phoneNumber = oneContactNumer.value as! CNPhoneNumber
  
                          contactNumber = phoneNumber.stringValue
  
                          let stringArray = contactNumber!.componentsSeparatedByCharactersInSet(
                              NSCharacterSet.decimalDigitCharacterSet().invertedSet)
                          let newString = "1" + stringArray.joinWithSeparator("")
  
                          let usersURL: Firebase! = Firebase(url: firebaseMainURL + "presentUserIDUserNameByPhoneNumber/" + newString)
  
                          // Check db:
  
                          usersURL.observeSingleEventOfType(.Value, withBlock: { snapshot in
  
                              if snapshot.childrenCount &gt; 1 {
  
                                  // They are users (but not your friends):
                                  self.friends.append(Friend(userName: snapshot.value["userName"] as! String, phoneNumber: snapshot.key, status: 1, name: "test", userID: snapshot.value["userID"] as! String))
  
                                  let userName = snapshot.value["userName"] as! String
  
                                  print("Friends name: " + userName)
  
                                  // Remove that contact
                                  self.contacts.removeObject(oneContact)
  
                              }
  
                          })
  
                      }
  
                  }
  
              })
  </code></pre>
  
  <p>How can I test and check when the second, on <code>usersURL</code>, has completed before allowing other actions to occur in app?</p>