title: Firebase reference call not returning proper object (Swift)
body: <p>I have a simple messaging app and the below function is being called and being passed the uid of the person who sent the message. The proper uid is being sent everytime the function is called, but the user is not the user related to that uid. Instead, it is returning the first user the number of times that person has messages and then returning the next user the number of times that person has messages.</p>
  
  <p>For example, if I have two users in a chat (Sue and Bob) and Sue has 10 messages total in this chat and Bob has 7, it returns Sue as the user for the first 10 messages and then Bob as the user for the final 7.</p>
  
  <pre><code>private func fetchUser(withId uid: String) {
  
      FIRDatabase.database().reference().child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
  
          if let dictionary = snapshot.value as? [String: AnyObject] {
              let user = User()
              user.name = dictionary["name"] as? String
              user.profileImageUrl = dictionary["profileImageURL"] as? String
              self.users.append(user)
              self.attemptReloadOfCollection()
          }
      }, withCancel: nil)
  }
  </code></pre>
  
  <p>Called From:</p>
  
  <pre><code>private func observeMessages() {
      guard let uid = FIRAuth.auth()?.currentUser?.uid else {
          return
      }
  
      let groupId = groupIdFromPreviousController
  
      let groupMessagesRef = FIRDatabase.database().reference().child("groups").child(groupId).child("messages")
      groupMessagesRef.observe(.childAdded, with: { (snapshot) in
          let messageId = snapshot.key
  
          let messagesRef = FIRDatabase.database().reference().child("all-messages").child(messageId)
          messagesRef.observeSingleEvent(of: .value, with: { (snapshot) in
  
              if let dictionary = snapshot.value as? [String: AnyObject] {
                  let message = Message(dictionary: dictionary)
  
                  guard let messageUserId = message.fromId else {
                      return
                  }
  
                  self.fetchUser(withId: messageUserId)
  
                  self.messages.append(message)
  
                  self.attemptReloadOfCollection()
  
                  DispatchQueue.main.async {
                      //scroll to the last index
                     // let indexPath = IndexPath(item: self.messages.count - 1, section: 0)
                     // self.collectionView?.scrollToItem(at: indexPath, at: .bottom, animated: true)
                  }
              }
  
          }, withCancel: nil)
  
      }, withCancel: nil)
  }
  </code></pre>
  
  <p>The function is getting called all of its times before the first call to database is made. So if I were to put a print(uid) above the Firebase call, it would print all uids before it makes a call to the database.</p>
  
  <p>Database Structure for users:
  [<img src="https://i.stack.imgur.com/Hzkac.png" alt="&quot;database structure users&quot;]"></p>
  
  <p>Database Structure for groups:
  [<img src="https://i.stack.imgur.com/vRaSm.png" alt="&quot;database structure groups&quot;]"></p>