全局变量未继承返回的 Firebase 数据 (Swift)
Global variable is not inheriting from returned Firebase data (Swift)
我正在尝试显示一组我从我的 firebase 数据库中读取的坐标。我通过函数检索纬度和经度坐标并将其分配给一个全局变量,以便我可以调用它在我的地图上显示。但是没有显示值,它显示原始值 (0.0, 0.0) 。我打印出我的结果并将结果分配给全局变量但是当我想显示它时它没有被继承。我可以实施哪些解决方法或快速修复?
var GPSlatdata = 0.0
var GPSlongdata = 0.0
func getlatitude(completion: @escaping (Double)->()){
let ref = Database.database().reference()
ref.child("GPSData/devices/1/latitude").observeSingleEvent(of: .value) { (snapshot) in
if let latitudefire = snapshot.value as? Double
{
completion(latitudefire)
}
}
}
func getlongitude(completion: @escaping (Double)->()){
let ref = Database.database().reference()
ref.child("GPSData/devices/1/longitude").observeSingleEvent(of: .value) { (snapshot) in
if let longitudefire = snapshot.value as? Double
{
completion(longitudefire)
}
}
}
getlatitude(completion: { (latx) in
print("Before: \(GPSlatdata)")
print("Recieved Lat Value: \(latx)")
GPSlatdata = GPSlatdata + latx
print("GPSlata data is = \(GPSlatdata)")
})
getlongitude(completion: { (longx) in
print("Beforelong: \(GPSlongdata)")
print("Recieved Lat Value: \(longx)")
GPSlongdata = GPSlongdata + longx
print("GPSlata data is = \(GPSlongdata)")
})
let skateboardposition = CLLocationCoordinate2D(latitude: GPSlatdata, longitude: GPSlongdata)
let skateboardposition = CLLocationCoordinate2D(latitude: x1, longitude: x2)
//just added
let skateboardAnnotation = GPSAnnotation(coordinate: skateboardposition, title: "ACES Skaters", subtitle: "Current Board location - \nLatitude: \(GPSlatdata) Longitude: \(GPSlongdata)")
GPSMap.addAnnotation(skateboardAnnotation)
GPSMap.setRegion(skateboardAnnotation.region, animated: true)
更新更改:仍然无法绘制坐标。
import UIKit
import Foundation
import MapKit
import Firebase
final class GPSAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
init(coordinate: CLLocationCoordinate2D, title: String?, subtitle: String?)
{
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
super.init()
}
var region: MKCoordinateRegion{
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
return MKCoordinateRegion(center: coordinate, span: span)
}
}
extension String {
func toDouble() -> Double? {
return NumberFormatter().number(from: self)?.doubleValue
}
}
class GPSMapViewController: UIViewController {
@IBOutlet weak var GPSMap: MKMapView!
@IBOutlet weak var latLabel: UILabel!
@IBOutlet weak var longLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let queue = DispatchGroup()
// Do any additional setup after loading the view.
var GPSlatdata = 0.0
var GPSlongdata = 0.0
let ref = Database.database().reference()
ref.child("GPSData/devices/1/latitude").observeSingleEvent(of: .value) { (snapshot) in
if let latitudefire = snapshot.value as? Double
{
self.latLabel.text = "\(latitudefire)"
}
}
ref.child("GPSData/devices/1/longitude").observeSingleEvent(of: .value) { (snapshot) in
if let longitudefire = snapshot.value as? Double
{
self.longLabel.text = "\(longitudefire)"
}
}
GPSMap.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
func getlatitude(completion: @escaping (Double)->()){
let ref = Database.database().reference()
ref.child("GPSData/devices/1/latitude").observeSingleEvent(of: .value) { (snapshot) in
if let latitudefire = snapshot.value as? Double
{
completion(latitudefire)
}
}
}
func getlongitude(completion: @escaping (Double)->()){
let ref = Database.database().reference()
ref.child("GPSData/devices/1/longitude").observeSingleEvent(of: .value) { (snapshot) in
if let longitudefire = snapshot.value as? Double
{
completion(longitudefire)
}
}
}
queue.enter()
getlatitude(completion: { (latx) in
print("Before: \(GPSlatdata)")
print("Recieved Lat Value: \(latx)")
GPSlatdata = GPSlatdata + latx
print("GPSlata data is = \(GPSlatdata)")
queue.leave()
})
queue.enter()
getlongitude(completion: { (longx) in
print("Beforelong: \(GPSlongdata)")
print("Recieved Lat Value: \(longx)")
GPSlongdata = GPSlongdata + longx
print("GPSlata data is = \(GPSlongdata)")
queue.leave()
})
DispatchQueue.main.async{
let skateboardposition = CLLocationCoordinate2D(latitude: GPSlatdata, longitude: GPSlongdata)
// let skateboardposition = CLLocationCoordinate2D(latitude: x1, longitude: x2)
print("The skateboard position is: \(skateboardposition)")
print("GPSlatdata is: \(GPSlatdata)")
print("GPSlongdata is: \(GPSlongdata)")
let skateboardAnnotation = GPSAnnotation(coordinate: skateboardposition, title: "ACES Skaters", subtitle: "Current Board location - \nLatitude: \(GPSlatdata) Longitude: \(GPSlongdata)")
self.GPSMap.addAnnotation(skateboardAnnotation)
self.GPSMap.setRegion(skateboardAnnotation.region, animated: true)
}
}
}
extension GPSMapViewController: MKMapViewDelegate{
func GPSMap(_ GPSMap: MKMapView, viewFor annotation: MKAnnotation) -> MKMarkerAnnotationView?{
if let GPSAnnotationView = GPSMap.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier) as? MKMarkerAnnotationView{
GPSAnnotationView.animatesWhenAdded = true
GPSAnnotationView.titleVisibility = .adaptive
GPSAnnotationView.titleVisibility = .adaptive
}
return nil
}
}
输出:仍在绘制坐标 (0.0, 0.0)
您没有等待完成处理程序完成。完成处理程序不会自动发生 - 您必须等待数据被检索。尝试使用 DispatchGroup
以便您可以确定事件何时完成。
let queue = DispatchGroup()
for i in 0..<5 {
//Here we add a child into a queue
queue.enter()
print("Entering")
//Start some action and wait for it to finish
//... It finishes
queue.leave()
print("Leaving")
}
queue.notify(queue: .main) {
print("We are done")
}
所以在你的情况下
let queue = DispatchGroup()
//Start an item
queue.enter()
getlatitude(completion: { (latx) in
print("Before: \(GPSlatdata)")
print("Recieved Lat Value: \(latx)")
GPSlatdata = GPSlatdata + latx
print("GPSlata data is = \(GPSlatdata)")
//we found latdata so leave
queue.leave()
})
//Start an item
queue.enter()
getlongitude(completion: { (longx) in
print("Beforelong: \(GPSlongdata)")
print("Recieved Lat Value: \(longx)")
GPSlongdata = GPSlongdata + longx
print("GPSlata data is = \(GPSlongdata)")
//we found longdata so leave
queue.leave()
})
//We are done with items once they BOTH finish.
queue.notify(queue: .main) {
let skateboardposition = CLLocationCoordinate2D(latitude: GPSlatdata, longitude: GPSlongdata)
//just added
let skateboardAnnotation = GPSAnnotation(coordinate: skateboardposition, title: "ACES Skaters", subtitle: "Current Board location - \nLatitude: \(GPSlatdata) Longitude: \(GPSlongdata)")
GPSMap.addAnnotation(skateboardAnnotation)
GPSMap.setRegion(skateboardAnnotation.region, animated: tr
}
参考文献:https://www.raywenderlich.com/5371-grand-central-dispatch-tutorial-for-swift-4-part-2-2
注意:要测试是否是这种情况,请在 let skateboardposition = CLLocationCoordinate2D(latitude: GPSlatdata, longitude: GPSlongdata)
之后放置一个打印语句,然后查看先打印哪些...新的打印语句或完成处理程序中的打印语句。
我正在尝试显示一组我从我的 firebase 数据库中读取的坐标。我通过函数检索纬度和经度坐标并将其分配给一个全局变量,以便我可以调用它在我的地图上显示。但是没有显示值,它显示原始值 (0.0, 0.0) 。我打印出我的结果并将结果分配给全局变量但是当我想显示它时它没有被继承。我可以实施哪些解决方法或快速修复?
var GPSlatdata = 0.0
var GPSlongdata = 0.0
func getlatitude(completion: @escaping (Double)->()){
let ref = Database.database().reference()
ref.child("GPSData/devices/1/latitude").observeSingleEvent(of: .value) { (snapshot) in
if let latitudefire = snapshot.value as? Double
{
completion(latitudefire)
}
}
}
func getlongitude(completion: @escaping (Double)->()){
let ref = Database.database().reference()
ref.child("GPSData/devices/1/longitude").observeSingleEvent(of: .value) { (snapshot) in
if let longitudefire = snapshot.value as? Double
{
completion(longitudefire)
}
}
}
getlatitude(completion: { (latx) in
print("Before: \(GPSlatdata)")
print("Recieved Lat Value: \(latx)")
GPSlatdata = GPSlatdata + latx
print("GPSlata data is = \(GPSlatdata)")
})
getlongitude(completion: { (longx) in
print("Beforelong: \(GPSlongdata)")
print("Recieved Lat Value: \(longx)")
GPSlongdata = GPSlongdata + longx
print("GPSlata data is = \(GPSlongdata)")
})
let skateboardposition = CLLocationCoordinate2D(latitude: GPSlatdata, longitude: GPSlongdata)
let skateboardposition = CLLocationCoordinate2D(latitude: x1, longitude: x2)
//just added
let skateboardAnnotation = GPSAnnotation(coordinate: skateboardposition, title: "ACES Skaters", subtitle: "Current Board location - \nLatitude: \(GPSlatdata) Longitude: \(GPSlongdata)")
GPSMap.addAnnotation(skateboardAnnotation)
GPSMap.setRegion(skateboardAnnotation.region, animated: true)
更新更改:仍然无法绘制坐标。
import UIKit
import Foundation
import MapKit
import Firebase
final class GPSAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
init(coordinate: CLLocationCoordinate2D, title: String?, subtitle: String?)
{
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
super.init()
}
var region: MKCoordinateRegion{
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
return MKCoordinateRegion(center: coordinate, span: span)
}
}
extension String {
func toDouble() -> Double? {
return NumberFormatter().number(from: self)?.doubleValue
}
}
class GPSMapViewController: UIViewController {
@IBOutlet weak var GPSMap: MKMapView!
@IBOutlet weak var latLabel: UILabel!
@IBOutlet weak var longLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let queue = DispatchGroup()
// Do any additional setup after loading the view.
var GPSlatdata = 0.0
var GPSlongdata = 0.0
let ref = Database.database().reference()
ref.child("GPSData/devices/1/latitude").observeSingleEvent(of: .value) { (snapshot) in
if let latitudefire = snapshot.value as? Double
{
self.latLabel.text = "\(latitudefire)"
}
}
ref.child("GPSData/devices/1/longitude").observeSingleEvent(of: .value) { (snapshot) in
if let longitudefire = snapshot.value as? Double
{
self.longLabel.text = "\(longitudefire)"
}
}
GPSMap.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
func getlatitude(completion: @escaping (Double)->()){
let ref = Database.database().reference()
ref.child("GPSData/devices/1/latitude").observeSingleEvent(of: .value) { (snapshot) in
if let latitudefire = snapshot.value as? Double
{
completion(latitudefire)
}
}
}
func getlongitude(completion: @escaping (Double)->()){
let ref = Database.database().reference()
ref.child("GPSData/devices/1/longitude").observeSingleEvent(of: .value) { (snapshot) in
if let longitudefire = snapshot.value as? Double
{
completion(longitudefire)
}
}
}
queue.enter()
getlatitude(completion: { (latx) in
print("Before: \(GPSlatdata)")
print("Recieved Lat Value: \(latx)")
GPSlatdata = GPSlatdata + latx
print("GPSlata data is = \(GPSlatdata)")
queue.leave()
})
queue.enter()
getlongitude(completion: { (longx) in
print("Beforelong: \(GPSlongdata)")
print("Recieved Lat Value: \(longx)")
GPSlongdata = GPSlongdata + longx
print("GPSlata data is = \(GPSlongdata)")
queue.leave()
})
DispatchQueue.main.async{
let skateboardposition = CLLocationCoordinate2D(latitude: GPSlatdata, longitude: GPSlongdata)
// let skateboardposition = CLLocationCoordinate2D(latitude: x1, longitude: x2)
print("The skateboard position is: \(skateboardposition)")
print("GPSlatdata is: \(GPSlatdata)")
print("GPSlongdata is: \(GPSlongdata)")
let skateboardAnnotation = GPSAnnotation(coordinate: skateboardposition, title: "ACES Skaters", subtitle: "Current Board location - \nLatitude: \(GPSlatdata) Longitude: \(GPSlongdata)")
self.GPSMap.addAnnotation(skateboardAnnotation)
self.GPSMap.setRegion(skateboardAnnotation.region, animated: true)
}
}
}
extension GPSMapViewController: MKMapViewDelegate{
func GPSMap(_ GPSMap: MKMapView, viewFor annotation: MKAnnotation) -> MKMarkerAnnotationView?{
if let GPSAnnotationView = GPSMap.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier) as? MKMarkerAnnotationView{
GPSAnnotationView.animatesWhenAdded = true
GPSAnnotationView.titleVisibility = .adaptive
GPSAnnotationView.titleVisibility = .adaptive
}
return nil
}
}
输出:仍在绘制坐标 (0.0, 0.0)
您没有等待完成处理程序完成。完成处理程序不会自动发生 - 您必须等待数据被检索。尝试使用 DispatchGroup
以便您可以确定事件何时完成。
let queue = DispatchGroup()
for i in 0..<5 {
//Here we add a child into a queue
queue.enter()
print("Entering")
//Start some action and wait for it to finish
//... It finishes
queue.leave()
print("Leaving")
}
queue.notify(queue: .main) {
print("We are done")
}
所以在你的情况下
let queue = DispatchGroup()
//Start an item
queue.enter()
getlatitude(completion: { (latx) in
print("Before: \(GPSlatdata)")
print("Recieved Lat Value: \(latx)")
GPSlatdata = GPSlatdata + latx
print("GPSlata data is = \(GPSlatdata)")
//we found latdata so leave
queue.leave()
})
//Start an item
queue.enter()
getlongitude(completion: { (longx) in
print("Beforelong: \(GPSlongdata)")
print("Recieved Lat Value: \(longx)")
GPSlongdata = GPSlongdata + longx
print("GPSlata data is = \(GPSlongdata)")
//we found longdata so leave
queue.leave()
})
//We are done with items once they BOTH finish.
queue.notify(queue: .main) {
let skateboardposition = CLLocationCoordinate2D(latitude: GPSlatdata, longitude: GPSlongdata)
//just added
let skateboardAnnotation = GPSAnnotation(coordinate: skateboardposition, title: "ACES Skaters", subtitle: "Current Board location - \nLatitude: \(GPSlatdata) Longitude: \(GPSlongdata)")
GPSMap.addAnnotation(skateboardAnnotation)
GPSMap.setRegion(skateboardAnnotation.region, animated: tr
}
参考文献:https://www.raywenderlich.com/5371-grand-central-dispatch-tutorial-for-swift-4-part-2-2
注意:要测试是否是这种情况,请在 let skateboardposition = CLLocationCoordinate2D(latitude: GPSlatdata, longitude: GPSlongdata)
之后放置一个打印语句,然后查看先打印哪些...新的打印语句或完成处理程序中的打印语句。