函数在完成 [Swift] 之前返回 [Firebase]
Function returning before completing [Swift] [Firebase]
我正在尝试从 Google Firebase 数据库中读取数据,并且我意识到我的函数在读取数据之前 returning。
这是代码
func getFirebaseCoordinates(increment : Int) -> CLLocationCoordinate2D {
var coordinates : CLLocationCoordinate2D = CLLocationCoordinate2D()
let ref = Database.database().reference()
ref.child("\(getPilot() + String(increment))/Latitude").observeSingleEvent(of: .value) { (snapshot) in
let lat = snapshot.value as? CLLocationDegrees
coordinates.latitude = lat ?? -1.0
print(coordinates.latitude)
}
ref.child("\(getPilot() + String(increment))/Longitude").observeSingleEvent(of: .value) { (snapshot) in
let long = snapshot.value as? CLLocationDegrees
coordinates.longitude = long ?? -1.0
print(coordinates.longitude)
}
print(coordinates)
return coordinates
}
当我检索坐标时,我打印它们,然后在函数结束时,我打印我正在 returning 的内容。我实际上循环了这个函数两次,在我什至可以打印出我检索到的第一个坐标之前,我已经打印了两个 return 语句,结果是 "CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)"
有什么想法吗?谢谢!
您需要 return 异步函数中的完成处理程序,因为 return 在您获得异步调用结果之前执行
func getFirebaseCoordinates(increment : Int,completion: @escaping (CLLocationCoordinate2D)-> Void ) {
var coordinates : CLLocationCoordinate2D = CLLocationCoordinate2D()
let ref = Database.database().reference()
ref.child("\(getPilot() + String(increment))/Latitude").observeSingleEvent(of: .value) { (latitudeSnapshot) in
let lat = latitudeSnapshot.value as? CLLocationDegrees
coordinates.latitude = lat ?? -1.0
ref.child("\(getPilot() + String(increment))/Longitude").observeSingleEvent(of: .value) { (LongitudeSnapshot) in
let long = LongitudeSnapshot.value as? CLLocationDegrees
coordinates.longitude = long ?? -1.0
completion(coordinates)
}
}
}
我正在尝试从 Google Firebase 数据库中读取数据,并且我意识到我的函数在读取数据之前 returning。 这是代码
func getFirebaseCoordinates(increment : Int) -> CLLocationCoordinate2D {
var coordinates : CLLocationCoordinate2D = CLLocationCoordinate2D()
let ref = Database.database().reference()
ref.child("\(getPilot() + String(increment))/Latitude").observeSingleEvent(of: .value) { (snapshot) in
let lat = snapshot.value as? CLLocationDegrees
coordinates.latitude = lat ?? -1.0
print(coordinates.latitude)
}
ref.child("\(getPilot() + String(increment))/Longitude").observeSingleEvent(of: .value) { (snapshot) in
let long = snapshot.value as? CLLocationDegrees
coordinates.longitude = long ?? -1.0
print(coordinates.longitude)
}
print(coordinates)
return coordinates
}
当我检索坐标时,我打印它们,然后在函数结束时,我打印我正在 returning 的内容。我实际上循环了这个函数两次,在我什至可以打印出我检索到的第一个坐标之前,我已经打印了两个 return 语句,结果是 "CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)" 有什么想法吗?谢谢!
您需要 return 异步函数中的完成处理程序,因为 return 在您获得异步调用结果之前执行
func getFirebaseCoordinates(increment : Int,completion: @escaping (CLLocationCoordinate2D)-> Void ) {
var coordinates : CLLocationCoordinate2D = CLLocationCoordinate2D()
let ref = Database.database().reference()
ref.child("\(getPilot() + String(increment))/Latitude").observeSingleEvent(of: .value) { (latitudeSnapshot) in
let lat = latitudeSnapshot.value as? CLLocationDegrees
coordinates.latitude = lat ?? -1.0
ref.child("\(getPilot() + String(increment))/Longitude").observeSingleEvent(of: .value) { (LongitudeSnapshot) in
let long = LongitudeSnapshot.value as? CLLocationDegrees
coordinates.longitude = long ?? -1.0
completion(coordinates)
}
}
}