Can I apply pagination on GeoFire Query to reduce load time

狂风中的少年 提交于 2020-07-18 08:38:05

问题


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:


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



来源:https://stackoverflow.com/questions/45912772/can-i-apply-pagination-on-geofire-query-to-reduce-load-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!