GeoFire + Swift 3 can't stop observing

Viewed 680

I'm using GeoFire and trying to fetch only 3 results that satisfy some conditions. This it my case and it doesn't stop observer. There are several thousands results and I get it all but I need only 3. I based on this answer but it does't work in my case as you can see. Please can somebody help?

var newRefHandle: FIRDatabaseHandle?
var gFCircleQuery: GFCircleQuery?

func findFUsersInOnePath(location: CLLocation,
                         radius: Int,
                         indexPath: String,
                         completion: @escaping () -> ()){
    var ids = 0
    let geofireRef = usersRef.child(indexPath)
    if let geoFire = GeoFire(firebaseRef: geofireRef) {
        gFCircleQuery = geoFire.query(at: location, withRadius: Double(radius))
        newRefHandle = gFCircleQuery?.observe(.keyEntered, with: { (key, location) in
            // if key fit some condition
            ids += 1
            if (ids >= 3) {
                self.gFCircleQuery?.removeObserver(withFirebaseHandle: self.newRefHandle!)
                completion()
            }
        })
        
        gFCircleQuery?.observeReady({
            completion()
        })
}

Please, never mind on Optionals(?) it is just for this example code

From GoeFire documentation:

To cancel one or all callbacks for a geo query, call removeObserverWithFirebaseHandle: or removeAllObservers:, respectively.

Both are not working.

1 Answers
Related