功能正在执行多次

时间:2017-06-10 06:33:14

标签: ios swift firebase firebase-realtime-database

我几天来一直在讨论这个问题。我有一个功能 1)获取快照值,然后将它们存储到filteredLocations数组中 2)获取用户在屏幕上按下的位置,然后测量他们在屏幕上按下的位置的两个位置点与快照中的位置之间的距离 3)过滤掉满足用户radiusDistance的位置,然后将点放在地图上

我不断遇到的问题是我的打印语句检查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: {

                })
            }
        })

}

0 个答案:

没有答案