func observeActiveTrackingState() -> 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
}
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
pathToObserveState.observeSingleEvent(of: .value, with: { (snapshot) in
})
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