【问题标题】:Geofire + Infinite Scroll - Can it Work?Geofire + Infinite Scroll - 可以吗?
【发布时间】:2017-03-16 00:33:45
【问题描述】:

我有一个应用程序,它通过 Geofire 的纬度/经度坐标获取存储在我的 Firebase 中的用户附近的位置。

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

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

感谢任何输入!

【问题讨论】:

    标签: javascript firebase geofire


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      你想diff 新的结果集与你已经得到的结果相比较,并且只处理新的键/值对。这可以使用array.filter 方法来实现。所以,vanilla js 真的。

      【讨论】:

      • 谢谢罗恩,我确实想出了一个普通的解决方案,它似乎足够有效地解决问题。我会尽快发布
      【解决方案3】:

      使用纯原版 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次

      【讨论】:

        猜你喜欢
        • 2015-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多