title: Return a variable within a completion block swift
body: <pre><code>func observeActiveTrackingState() -&gt; String{
  
      let pathToObserveState = databaseRef.child("locationState").child("\(userID!)")
  
      pathToObserveState.observeSingleEvent(of: .value, with: { (snapshot) in
  
          let snapshotValue = snapshot.value as? NSDictionary
  
          print("Snapshot Returned (ObserveActiveTrackingState) = \(snapshotValue?["state"] as! String)")
  
          let currentState = snapshotValue?["state"] as! String
  
          return currentState //*****GIVES ME AN ERROR*****
  
          })
      //IF I PUT THE RETURN HERE HOW DO I CALL THE CURRENTSTATE VARIABLE
  }
  </code></pre>
  
  <p>I have a function called "observeActiveTrackingState" that is supposed to return a string and typically when add a return it returns the string variable that I want however since I need to put the return statement under</p>
  
  <pre><code>pathToObserveState.observeSingleEvent(of: .value, with: { (snapshot) in
  
  
      }) 
  </code></pre>
  
  <p>It returns an error and says it needs to happen outside of firebase's completion block. How do I fix this code so that when I call the function I get the variable that I want</p>