Geofire + Infinite Scroll - 它可以工作吗?

时间:2017-03-16 00:33:46

标签: javascript firebase geofire

我有一个应用程序可以通过Geofire的lat / long坐标获取存储在Firebase中的用户周围的附近位置。

我现在希望能够在用户滚动到位置列表的底部(也称为无限滚动)时获取更多位置。

为此,我增加了半径,从而获取更多位置 - 但是我想避免获取已经加载的相同位置。 Geofire是否支持这样的任何内容(即,在查询中排除已经加载的位置),还是必须通过一些手动处理来处理?

感谢任何输入!

3 个答案:

答案 0 :(得分:1)

Geofire将始终返回属于Geoquery范围内的所有键。因此,如果您为越来越大的范围创建地理查询,您将获得重叠键。

但是Geofire中的键非常小,通常比键所代表的实际对象小很多。因此,如果您确保不重新加载已加载的对象,则可能仍然可行。

答案 1 :(得分:1)

您希望diff新结果集与您已经获得的结果相对应,并且只处理新的键/值对。这可以使用array.filter方法实现。所以,vanilla js真的。

答案 2 :(得分:0)

使用纯粹的vanilla javascript解决方案,我只能使用以下内容添加新位置:

$scope.locations = ... // returned from service method
var existingKeys = []; // keep track of existing firebase keys

$scope.locations.forEach(function(location){ // Store the initial location ids
  existingKeys.push(location.id);
});

locationService.getNearby(5) // Set 3km as the new radius (was 2km)
  .then(function(moreLocations){
    moreLocations.forEach(function(newLocation){

    /** Prevents pushing duplicates into array by comparing the existing keys **/
    if(existingKeys.indexOf(newLocation.id) === -1 ){
      $scope.locations.push(newLocation);
      existingKeys.push(newLocation.id); // Append new keys to existingKeys[] 
    }
})

注意 *这阻止重新加载相同的对象x次