我很难在返回函数中使用 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
}
最佳答案
您已经暗示了您的问题,但没有明确说明。交易是您不能将异步函数的结果作为函数结果返回。您需要传入一个在函数完成时运行的完成处理程序,该代码是有权访问结果的代码。
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.
关于ios - Swift Firebase 函数返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42894472/