【问题标题】:Remove and re-insert a GeoFire location to trigger onKeyExited and onKeyEntered doesn't work删除并重新插入 GeoFire 位置以触发 onKeyExited 和 onKeyEntered 不起作用
【发布时间】:2016-09-16 16:30:37
【问题描述】:

我遇到了 GeoFire 等问题,Firebase。

我想根据 GeoFire 位置触发器更新 UI 上的信息。当客户端发生动作时,必须将数据保存在 Firebase 数据库中,并且必须更新 GeoFire。

我会用一些代码更清楚。

1- 客户端的操作。存储用户及其位置的温度。

public void sendTemperature(Float temp) {

    LatLng myPosition = getMyPosition();

    //instantiate the db connection
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference dbTemperatures = database.getReference("temperatures");

    //push the value to global temperature db
    String tempId = dbTemperatures.push().getKey();
    dbTemperatures.child("/"+tempId).setValue(temp);

    //create an index to link the user with its last sent temperature
    DatabaseReference dbUserLastTemperature = database.getReference("users_last_temperature");
    dbUserLastTemperature.child(userId).setValue(tempId);

    //update the geofireDb (here is the problem)
    geoUsersTemperatures.removeLocation(userId);
    geoUsersTemperatures.setLocation(userId, new GeoLocation(myPosition.latitude, myPosition.longitude));

}

2 - 客户端的侦听器。获取附近用户温度的通知。

    GeoQuery usersTemperatureGeoQuery = geoUsersTemperatures.queryAtLocation(new GeoLocation(getMyPosition().latitude, getMyPosition().longitude), mapsRadius);
    usersTemperatureGeoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {
            System.out.println(String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude));
            addTemperature(key, location);
        }

        @Override
        public void onKeyExited(String key) {
            System.out.println(String.format("Key %s is no longer in the search area", key));
            removeTemperature(key);
        }

        @Override
        public void onKeyMoved(String key, GeoLocation location) {
            System.out.println(String.format("Key %s moved within the search area to [%f,%f]", key, location.latitude, location.longitude));
            moveTemperature(key, location);
        }

        @Override
        public void onGeoQueryReady() {
            System.out.println("All initial data has been loaded and events have been fired!");
        }

        @Override
        public void onGeoQueryError(DatabaseError error) {
            System.err.println("There was an error with this query: " + error);
        }
    });

3- 问题。我期望发生的是,当发送新温度时,侦听器会检测到它的删除和新插入(但实际上它的键没有改变),因此我可以更新我的 UI。这不会发生,因为这两个动作是如此之快,以至于 GeoFire 侦听器不会触发任何东西,因为它在 db 中没有任何变化。

有没有办法让听者按照我的意愿行事?

【问题讨论】:

    标签: android firebase firebase-realtime-database geofire


    【解决方案1】:

    这可能可以通过在removeLocation() 中使用所谓的completion listener 来解决。

    快速、未经测试的文章(因此可能包含语法错误):

    geoUsersTemperatures.removeLocation(userId, new GeoFire.CompletionListener {
      public void onComplete(String key, DatabaseError error) {
        geoUsersTemperatures.setLocation(userId, new GeoLocation(myPosition.latitude, myPosition.longitude));
      });
    });
    

    使用这种方法,您要等到数据库确认已删除旧位置,然后再设置新位置。

    【讨论】:

      猜你喜欢
      • 2019-03-25
      • 2018-12-02
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 2011-05-05
      • 2019-09-12
      • 1970-01-01
      相关资源
      最近更新 更多