GeoFire swift 3 query not returning results in the order expected from GeoFire/FireBase

422 views Asked by At

Here is the relevant function in my ViewController:

@IBAction func findPeople(_ sender: Any) {
    let center = CLLocation(latitude: myLocation.latitude, longitude: myLocation.longitude)
    let radiusQuery = geoFireUserLocations!.query(at: center, withRadius: 0.05)

    print("Start Looking for People")

    _ = radiusQuery!.observe(.keyEntered, with: { (key: String?, location: CLLocation?) in
        print("  ALERT:  Found Someone Close : ",key!)
    })

    _ = radiusQuery?.observeReady({
        print("  All initial data has been loaded and events have been fired!")
    })

    print("Done Looking for People")

    //TODO: Based on the GeoFire result do something smart   
}

The operation works, but the response comes in an unexpected order. I thought that the .observeReady call would return the initial set of results, but it does not return anything right away, and execution continues on to the line that prints "Done Looking for People".

Is there any way to have my function block until it gets an initial set of results?

Here's the output that I get:

Start Looking for People
Done Looking for People
     ALERT:  Found Someone Close :  oSf00ex6SyMAwpF2NRxymyxxx123
        All initial data has been loaded and events have been fired!

I was expecting:

Start Looking for People
     ALERT:  Found Someone Close :  oSf00ex6SyMAwpF2NRxymyxxx123
        All initial data has been loaded and events have been fired!
Done Looking for People

How can I create a function that blocks until it gets an initial result from GeoFire?

I'd like to send the request, get a result, then take the appropriate action based on the result. The way the app is currently behaving there does not seem to be a way to have it wait for a result.

Can anyone offer some insight and/or suggestion about how to make this work the way I'm describing? Is there some sort of blocking call that I could call or a synchronization method that I could be using to ensure that I get a result before deciding the next action?

Cheers!

0

There are 0 answers