geofire returns duplicates keys when expanding radius in swift

时间:2018-11-21 06:54:58

标签: swift geofire

I am trying to implement infinite scrolling by expanding the radius of my GeoFire query by 5kms so I can get the next set of keys. But my GeoFire query is returning duplicates as it is picking up the keys found within the first 5kms in addition to the next 5kms so-on and so-on...

I was under the impression that it should only return the new keys found within the updated radius, have I missed something?

Console output showing duplicate keys found at 10kms then 15kms radius:

enter image description here

我的geoQuery

let geoQuery = GeoFire(firebaseRef: DB_BASE.child("venues-location")).query(at: CLLocation(), withRadius: 1)

我的fetchVenueNearby函数:

func fetchVenueNearby(deviceLocation: CLLocation, radius: Double, handler: @escaping (Venue) -> ()) {

        geoQuery.center = deviceLocation
        geoQuery.radius = radius

        geoQuery.observe(.keyEntered) { (key: String!, venueLocation: CLLocation!) in 

            print ("FOUND KEY: ", key) 

            self.REF_VENUES.child(key).observeSingleEvent(of: .value, with: { (snapshot) in 

                if let dictionary = snapshot.value as? [String : Any] {

                    //parse snapshot code here….

                }//end if-let

            })//end observeSingleEvent

        }//end geofire query observe


    }//end func

这是我的loadMoreVenues函数,当前用于测试我的地理位置查询

func loadMoreVenus() {

    radius += 5

    print("radius = \(radius)")


    DataService.run.fetchVenueNearby(deviceLocation: currentLocation, radius: radius) { (venue) in
        print("venue: \(venue.venueID)")
    }


}//end func

1 个答案:

答案 0 :(得分:0)

您需要过滤掉已经存在的结果。

当您使用新参数 + 5km 请求新结果时,它将向您返回新结果,您必须比较已有的结果并将其从结果中删除。

但是我对GeoFire并不了解很多。

更好的选择是基于获取的最新位置作为起点,而不是用户当前的位置来请求最新的结果,而无需进行过滤