imperiumlabs / GeoFirestore-iOS

GeoFirestore for iOS - Realtime location queries with Firestore
MIT License
127 stars 46 forks source link

KeyEntered Event Occurs Outside of Query #17

Open upsidedownone opened 5 years ago

upsidedownone commented 5 years ago

I am having trouble getting this to work with my project. KeyEntered events get fired when they are not in the query. When debugging the code, I can see that in the updateLocations() function the info.isInQuery is set to false, but the event is still fired? It seems like once a document enters the search criteria (which is already larger than it should be by around half a mile?) it always fires a KeyEntered event, even after query.region is updated. It also occasionally returns two of the same object for key entered events.

I'm not exactly sure what I am doing wrong. Here is where I set up the query:

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation:CLLocation = locations[0] as CLLocation

        self.currentLocation = CLLocation(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)

        if self.activeSearchQuery == "Current Location" && self.initialLocationsUpdate == true {
            centerMapOnLocation(location: currentLocation)
            self.regionQuery = GlobalDatabase.geoFirestore.query(inRegion: self.mapView.region)
            let queryHandle = self.regionQuery.observe(.documentEntered, with: { (key, location) in
                print("The document with documentID '\(key)' entered the search area and is at location '\(location)'")
                // Get document here
                }
            })
            self.initialLocationsUpdate = false
        }
    }

Then I update the region when the user moves the map location: self.regionQuery.region = self.mapView.region Does anybody know what I am doing incorrectly?

upsidedownone commented 5 years ago

Update: The comment here: https://github.com/imperiumlabs/GeoFirestore-iOS/issues/5#issuecomment-425996399 is what worked for me also. Note that all 3 events in updateLocations need to be modified, including the for the exit event. Not sure if I have something wrong in my setup, but making that change solved the issue. The .KeyEntered events are fired correctly and only when within the region.