Swift Firebasefunction返回

我在返回函数中遇到Firebase值很困难。 对我来说,这似乎是一个持续存在的问题。 我已经写了一个关于我的问题的基本例子。 我该怎么做呢?

func getChartIndexValues(completion:@escaping (Double) -> ()) { //Firebase Initialization var ref: FIRDatabaseReference! ref = FIRDatabase.database().reference() ref.child("general_room_index").observeSingleEvent(of: .value, with: {(snapshot) in let snapDict = snapshot.value as? NSDictionary var zero = snapDict?["0"] as! Double completion(zero) }) } returnFunction() -> (Double) { getChartIndexValues() { (zero) -> () in let testValue = zero } return //THis is my problem } 

你已经暗示了你的问题,但没有明确说明。 这笔交易是你不能将async函数的结果作为函数结果返回。 您需要传入一个在函数完成时运行的完成处理程序,并且该代码是可以访问结果的代码。

 returnFunction() -> (Double) { getChartIndexValues() { (zero) -> () in //Your code to process the results belongs here self.someInstanceVar = zero someLabel.text = "Your result is \(zero)" } } //You CAN'T put your code to process results here.