Can I apply pagination on GeoFire Query to reduce load time

851 views Asked by At

I have used GeoFire to fetch location based data from my Firebase database. I know if I set less radius in my query then I am able to load data quickly, but my requirement is that I want shorted data based on location, so nearest records shows first, and so on. So I have passed current location in GeoFire query with total earth radius, because I want all data. But I don't how to apply pagination with GeoFire, so in future when more records are available in Firebase database my current implementation will definitely takes more time to load.

Below is the code snipped which I have used to get location based records.

        let eartchRadiusInKms = 6371.0
        let geoFire = GeoFire(firebaseRef: databaseRef.child("items_location"))

        let center = CLLocation(latitude: (self.location?.coordinate.latitude)!, longitude: (self.location?.coordinate.longitude)!)
        let circleQuery = geoFire?.query(at: center, withRadius: eartchRadiusInKms)

        if CLLocationCoordinate2DIsValid(center.coordinate) {
            circleQuery?.observeReady({

                let myPostHandle : DatabaseHandle = circleQuery?.observe(.keyEntered, with: { (key: String?, location: CLLocation?) in

                    // Load the item for the key
                    let itemsRef = self.databaseRef.child("items").child(key!)
                    itemsRef.observeSingleEvent(of: .value, with: { (snapshot) in
                        // Manage Items data
                    })
                })
            })

        }

So can pagination is possible with GeoFire? Or I have to use some different mechanism, can anyone please advise me on this scenario?

1

There are 1 answers

6
Moaz Khan On BEST ANSWER

I faced a similar issue and I actually loaded a small radius first, then increased the radius and loaded another chunk of data in a background service. In the completion of the call I reloaded my data in collection view using

collectionView.reloadData()`

Here is how you query in geofire

self.circleQuery = self.geoFire?.query(at: myLocation, withRadius: myRadius)

self.circleQuery?.observe(.keyEntered, with: { (key: String?, location: CLLocation?) in ....

check out this function in geofire documentation. It keeps a track of the new entered locations in background. Now as you want pagination consider an example of tableView, you can just call this on onScroll

myRadius = myRadius + 1000 // or any number to increase radius

As the keyEntered observer is already set so it will return you back the new results. Just add them to your list and update the table / collection view