Ios 函数被多次执行

Ios 函数被多次执行,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,我已经为这个问题绞尽脑汁好几天了。我有一个函数 1) 获取快照值,然后将其存储到filteredLocations数组中 2) 获取用户在屏幕上按下的位置,然后测量他们在屏幕上按下位置的两个位置点与快照位置之间的距离 3) 过滤出符合用户半径距离的位置,然后在地图上放置点 然而,我经常遇到的问题是我的打印语句检查1和检查2。他们被执行了两次,而本应只执行一次 func addAnnotation(gestureRecognizer:UIGestureRecognizer){

我已经为这个问题绞尽脑汁好几天了。我有一个函数 1) 获取快照值,然后将其存储到filteredLocations数组中 2) 获取用户在屏幕上按下的位置,然后测量他们在屏幕上按下位置的两个位置点与快照位置之间的距离 3) 过滤出符合用户半径距离的位置,然后在地图上放置点

然而,我经常遇到的问题是我的打印语句检查1和检查2。他们被执行了两次,而本应只执行一次

    func addAnnotation(gestureRecognizer:UIGestureRecognizer){
        filteredLocations.removeAll()
        locations.removeAll()


        let sampleRef = FIRDatabase.database().reference().child("SamplePost").child("post")

        sampleRef.observeSingleEvent(of:.value, with: {(snapshot) in
            if  let result = snapshot.children.allObjects as? [FIRDataSnapshot] {
                for child in result{
                    let dictionary = child.value as? [String: AnyObject]
                    let lat = dictionary?["lat"] as! Double
                    let long = dictionary?["long"] as! Double
                    let image = dictionary?["image"] as! String
                    let text = dictionary?["text"] as! String
                    let user = dictionary?["user"] as! String

                    let structure = MapPoints(Latitude: lat, Longitude: long, Image: image, Text: text, User: user)
                    self.filteredLocations.append(structure)
                    self.locations.append(structure)


                    print("check one \(self.filteredLocations.count)")


                    self.collectionView.reloadData()
                }
            }
            let refined = Double(self.radiusDistanceNumber!)


            print("check two \(self.filteredLocations.count)")



            if gestureRecognizer.state == UIGestureRecognizerState.began{
                self.removePointsAndClearArray()
                let touchPoint = gestureRecognizer.location(in: self.mapView)
                let newCoordinates = self.mapView.projection.coordinate(for: touchPoint)
                let marker = GMSMarker(position: newCoordinates)
                marker.title = "Selected location"

                self.filteredLocations = self.locations.filter {
                    (locations) in

                    let userLocation = newCoordinates
                    let userLat = userLocation.latitude
                    let userLong = userLocation.longitude

                    let coordinateOne = CLLocation(latitude: userLat, longitude: userLong)
                    let coordinateTwo = CLLocation(latitude: CLLocationDegrees(locations.latitude!), longitude: CLLocationDegrees(locations.longitude!))

                    let distanceFromPoints = coordinateOne.distance(from: coordinateTwo)
                    let convertToMiles = distanceFromPoints*0.00062137
                    return convertToMiles < refined
                }
                print("check three \(self.filteredLocations.count)")
                self.filteredLocations.map {
                    (location) in
                    let annotation = GMSMarker()
                    annotation.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(location.latitude!), longitude: CLLocationDegrees(location.longitude!))
                    annotation.map = self.mapView
                    let camera = GMSCameraPosition.camera(withTarget: newCoordinates, zoom: 11)
                    self.mapView.animate(to: camera)
                }
                print("amount that is being counted \(self.filteredLocations.count)")
                if self.filteredLocations.count <= 0 {
                    self.collectionView.isUserInteractionEnabled = false
                } else{
                    self.collectionView.isUserInteractionEnabled = true
                }
                print("amount before reloading collectionView \(self.filteredLocations.count)")

                self.collectionView.reloadData()
                UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {

                })
            }
        })

}
func addAnnotation(GestureRecognitor:UIGestureRecognitor){
filteredLocations.removeAll()
locations.removeAll()
让sampleRef=FIRDatabase.database().reference().child(“SamplePost”).child(“post”)
sampleRef.observeSingleEvent(的值为:),其中:{(快照)位于
如果let result=snapshot.children.allObjects as?[FIRDataSnapshot]{
为了孩子{
让dictionary=child.value为?[字符串:AnyObject]
让lat=字典?[“lat”]为!双精度
让long=dictionary?[“long”]as!加倍
让image=dictionary?[“image”]作为!字符串
让text=字典?[“text”]作为!字符串
让用户=字典?[“用户”]作为!字符串
let structure=MapPoints(纬度:纬度,经度:长,图像:图像,文本:文本,用户:用户)
self.filteredLocations.append(结构)
self.locations.append(结构)
打印(“检查一个\(self.filteredLocations.count)”)
self.collectionView.reloadData()
}
}
让精炼=加倍(自半径距离数!)
打印(“检查两个\(self.filteredLocations.count)”)
如果gestureRecognizer.state==UIGestureRecognizerState.Start{
self.removePointsAndClearArray()
让touchPoint=GestureRecognitor.location(在:self.mapView中)
设newCoordinates=self.mapView.projection.coordinate(for:touchPoint)
让标记器=GMSMarker(位置:新坐标)
marker.title=“所选位置”
self.filteredLocations=self.locations.filter{
(地点)在
让userLocation=newCoordinates
让userLat=userLocation.latitude
让userLong=userLocation.longitude
let coordinateOne=CLLocation(纬度:userLat,经度:userLong)
让coordinateWO=CLLocation(纬度:CLLocationDegrees(位置.纬度!),经度:CLLocationDegrees(位置.经度!))
设distanceFromPoints=coordinateOne.距离(from:CoordinateTo)
让convertToMiles=距离点*0.00062137
返回转换文件
}
打印(“检查三个\(self.filteredLocations.count)”)
self.filteredLocations.map{
(地点)在
let annotation=GMSMarker()
annotation.position=CLLocationCoordinate2D(纬度:CLLocationDegrees(位置.纬度!)),经度:CLLocationDegrees(位置.经度!))
annotation.map=self.mapView
让camera=GMSCameraPosition.camera(目标:新坐标,缩放:11)
self.mapView.animate(到:摄影机)
}
打印(“正在计数的金额\(self.filteredLocations.count)”)

如果self.filteredLocations.count在复选框一和复选框二中打印您的
gestureerecognizer.state
,我打赌您将获得两种状态。可能开始和结束了?是的!!!!就是这样,我从一开始就移动了gestureerecognizer.state复选框,并且成功了,非常感谢@NateBirkholzPrint您在che中打印您的
gestureegnizer.state
勾选一和勾选二,我打赌你会得到两种状态。可能开始和结束了?是的!!!!就是这样,我移动了手势识别器。状态勾选一开始就成功了,非常感谢@NateBirkholz