Search code examples
iosswiftfirebasegeofire

GeoFire observeBlock are only called once in Swift project


This is my simplified code for GeoFire observing: The problem is that observeReady are only called once, on initial observation, however when new keys are entered, the observe function is working fine, but completion block is not getting called.

func startObserveNearbyPosts(location: CLLocation) {
        let geoFireQuery = geoFire?.query(at: location, withRadius: 100)

        geoFireQuery?.observe(.keyEntered, with: { (key, _) in
            print("Key entered")
        })

        geoFireQuery?.observeReady {
            print("Observe ready")
        }
    }

Solution

  • From the Geofire documentation:

    Sometimes you want to know when the data for all the initial keys has been loaded from the server and the corresponding events for those keys have been fired.

    So it looks like observeReadyWithBlock only gets called after the initial set of .keyEntered has been fired, not for subsequent updates (unless you change the query). Of course .keyEntered should get called for each key that enters the queried range.