本文介绍了我可以在GeoFire查询上应用分页以减少加载时间吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用GeoFire从我的Firebase数据库中获取基于位置的数据.我知道如果我在查询中设置的半径较小,那么我可以快速加载数据,但是我的要求是我希望根据位置缩短数据,因此首先显示最近的记录,依此类推.因此,我已在GeoFire查询中通过当前地球总半径传递了当前位置,因为我需要所有数据.但是我不使用GeoFire进行分页,因此将来在Firebase数据库中有更多记录可用时,我当前的实现肯定会花费更多时间来加载.

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
                    })
                })
            })

        }

那么GeoFire是否可以进行分页?还是我必须使用一些不同的机制,有人可以在这种情况下给我建议吗?

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

推荐答案

我遇到了类似的问题,实际上我先加载了一个较小的半径,然后又增加了半径并在后台服务中加载了另一块数据.通话结束后,我使用

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()`

这是您在geofire中进行查询的方式

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 ....

在geofire文档中查看此功能.它在后台跟踪新输入的位置.现在,如果您想分页考虑tableView的示例,只需在onScroll上调用它

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//或增加半径的任何数字

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

由于已经设置了keyEntered观察者,因此它将返回您新的结果.只需将它们添加到您的列表中,然后更新表格/集合视图

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

这篇关于我可以在GeoFire查询上应用分页以减少加载时间吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 13:40