1

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?

ChrisM
  • 1,576
  • 6
  • 18
  • 29
Dhaval Dobariya
  • 171
  • 1
  • 12
  • As far as I know pagination is not supported by default in GeoFire so you will have to use your own mechanism. One solution is to use small radius on first call and increase it with every next call. I don't see any other clean solution. We wanted to use GeoFire in our project too but since it was very slow for our needs and used too many calls we ended up writing our own API. – ZassX Aug 28 '17 at 06:59

1 Answers1

3

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

Moaz Khan
  • 1,272
  • 1
  • 13
  • 28
  • Can you please share some code snippet, so i can get better idea. Because as per my thinking if i do so, i may have duplicate data because the records which comes say in first 10 miles that are also came in 20 miles. Because GeoFire not accepting range for radius. So when radius increases, the old records are also fetched, so there is no use of that kind of process. – Dhaval Dobariya Aug 28 '17 at 10:07
  • 1
    No it won't duplicated the data as you are not changing the query you will just be changing the radius. GeoFire already handles that so don't worry about it. I am editing my post and to include some code snippets. – Moaz Khan Aug 28 '17 at 10:19
  • 1
    @DhavalDobariya don't generate new query when you want to get new location in increased radius, instead take your query object and set its radius to new value, when your data from previous radius ends (pagination). this will avoid data duplication – Umar Hussain Aug 28 '17 at 10:35
  • Thanks guys, your answers really helps me a lot. – Dhaval Dobariya Aug 29 '17 at 09:22
  • If it helped you please up vote and mark my answer as correct. Thanks – Moaz Khan Aug 29 '17 at 09:56
  • Hi guys, I know this dates a little but I was wondering how you solved this issue, I am currently struggling to getting this to work :/ – Roggie Nov 21 '18 at 03:28