Я пытаюсь сделать снимок данных, которые хранятся под Auto ID в базе данных.У меня все работает хорошо и данные видны.Единственная проблема заключается в том, что его цикл и функция с течением времени вызывают ошибку потока 1.Я дважды проверил, что я вызывал функцию только один раз, и это в ViewDidLoad.
Вот функция:
func loadInfo() {
ref = Database.database().reference()
locationData.removeAll()
ref.child("location").observe(.childAdded) { (snapshot: DataSnapshot) in
// will iterate through each child of "location" branch
if let dict = snapshot.value as? [String: AnyObject] {
let titleT = dict["Title"] as! String
let activity = dict["Activity"] as! String
let latitude = dict["Lattitude"] as! String
let long = dict["Longitude"] as! String
let key = dict["id"] as! String
let userlat = (self.locationManager.location?.coordinate.latitude)!
let userlong = (self.locationManager.location?.coordinate.longitude)!
let calcCoord0 = CLLocationCoordinate2DMake(CLLocationDegrees(latitude)!, CLLocationDegrees(long)!)
let calcCoord1 = CLLocationCoordinate2DMake(userlat, userlong)
var distance = Double()
print("user lat \(userlat)")
print("userLong \(userlong)")
let origin = Waypoint(coordinate: calcCoord0, coordinateAccuracy: -1, name: "Start")
let destination = Waypoint(coordinate: calcCoord1, coordinateAccuracy: -1, name: "Finish")
// Specify that the route is intended for automobiles avoiding traffic
let options = NavigationRouteOptions(waypoints: [origin, destination], profileIdentifier: .automobileAvoidingTraffic)
_ = Directions.shared.calculate(options) { [unowned self] (waypoints, routes, error) in
distance = Double(round(1000*(routes?.first!.distance)!).rounded()/1000) / 1000
self.locationData.insert(Locations(title: titleT, activity: activity, geoCordinate: distance, lat: latitude, long: long, key: key), at: 0)
}
}
self.searchCV.reloadData()
}
}
Операторы печати были включены, чтобы увидеть, была ли функция взацикливание фактов.
Пожалуйста, может кто-нибудь помочь.Я просто хочу, чтобы функция прекратила цикл.Я хочу, чтобы информация была снята только один раз.