title: Why is Firestore's getDocument method returning an empty dictionary?
body: <p>I am making an iOS app in Swift with user data stored in a Firebase Firestore database. The user documents in Firestore are named by the UID's of the users. To get the data from Firestore, I have the function getUserData:</p>
  <pre><code>static func getUserData(uid: String) -&gt; [String : Any] {
          
          let userRef = Firestore.firestore().collection(Constants.Firestore.Collections.users)
          let userDocRef = userRef.document(uid)
          
          var temp: [String : Any] = [:]
          
          userDocRef.getDocument { (document, error) in
              guard let document = document, document.exists else {
                  print(&quot;document does not exist&quot;)
                  return
              }
                  
              let dataDesc = document.data()
              
                  
              temp = dataDesc!
  
          }
      
          return temp
          
      }
  </code></pre>
  <p>I call this method in an initializer for my user class:</p>
  <pre><code>class User {
      // MARK: - Properties
      var firstName: String
      var lastName: String
      let uid: String
      let phone: String
      var available: Bool
      var friends: [User] = []
  
      init?(firestoreUID: String) {
                  
          self.uid = firestoreUID
          
          let data = UserService.getUserData(uid: firestoreUID)
          print(data.keys)
          
          guard let phone = data[Constants.Firestore.Keys.phone] as? String,
              let available = data[Constants.Firestore.Keys.available] as? Bool,
              let firstName = data[Constants.Firestore.Keys.firstName] as? String,
              let lastName = data[Constants.Firestore.Keys.lastName] as? String
              else {
                  return nil
          }
          
          self.phone = phone
          self.available = available
          self.firstName = firstName
          self.lastName = lastName
      }
  }
  </code></pre>
  <p>However, when I unwrap the user instance, it gives a nil value. When I printed the keys of the retrieved dictionary from the getUserData function, the console shows only an empty array. What am I doing wrong, and how can I fix this so that I actually get my users' data from Firestore?</p>
  <p>Edit: I've changed getUserData to boss's answer, but I'm not sure what should be in my User class init. I tried the call format suggested with ```
  UserService.getUserData(uid: firestoreUID) { (data) in</p>
  <pre><code>        if let data = data {
              print(data.keys)
              temp = data
              
          } else {
              print(&quot;User not found&quot;)
              return
          }
          
      }
  </code></pre>
  <p>But I still get a &quot;Fatal error: Unexpectedly found nil when unwrapping an optional value&quot; when I try to query the temp dictionary.</p>