如果未创建Geofire,如何检查Geofire以注销用户?

时间:2019-03-26 19:08:59

标签: android firebase-realtime-database geofire

问题摘要:我是使用Geofire的Android Studio和Firebase的新手,请保持谦虚。我的目标是在用户不在我的应用的地图屏幕上但没有Geofire位置的情况下注销该用户。我无法弄清楚如何在我的Geofire中查询“ g”。如何查询“ g”子项以查看它是否已创建?如果未创建“ g”,则需要注销它们。因此,目标是查询“ g”并创建方法,如果“ g”为空,则将用户注销。

我已经完成的事情:许多Stackoverflow帖子并未使用Android Studio。我确实找到了这个I have inserted location using geofire in firebase,但无法解决我的问题。我也尝试过Google firebase check if child exists

onLocationChanges是我添加在线供应商的地方,并且我还想检查一下是否创建了“ g”。

 @Override
public void onLocationChanged(Location location) {


    // Adds VendorOnline Child to Firebase when Vendor is On This Activity
    addVendorOnline();
    // Log user out if they are on the map screen without a "VendorOnline" Uid
    logVendorOutIfBuggy();

}

这是创建在线供应商并检查是否已创建“ g”的方法:

 private void addVendorOnline(){

    if (FirebaseAuth.getInstance().getCurrentUser() != null) {

        String vendorId = FirebaseAuth.getInstance().getCurrentUser().getUid();
        DatabaseReference vendorIdReference = FirebaseDatabase.getInstance().getReference("VendorOnline");
        GeoFire vendorGeoFire = new GeoFire(vendorIdReference);
        vendorGeoFire.setLocation(vendorId, new GeoLocation(lastLocation.getLatitude(), lastLocation.getLongitude()));
    }
    }  


   // Log user out if for some reason they do not have a "g" child but are on the map screen.
   private void logVendorOutIfBuggy() {
   DatabaseReference ref = FirebaseDatabase.getInstance().getReference("g");

    if(ref == null){

        Toast.makeText(VendorMapsActivity.this, "Error", Toast.LENGTH_LONG).show();

        FirebaseAuth.getInstance().signOut();
    }

我期望发生的事情和实际发生的事情:我希望如果“ g”等于null,则该用户将被注销,但是现在该用户不会注销。

how to query for g

1 个答案:

答案 0 :(得分:1)

下面是调用getLocation来查看供应商是否注册位置的示例。

来自LocationCallback.onLocationResult的javadocs:

  

使用密钥的当前位置调用此方法。位置   如果密钥的GeoFire中没有存储任何位置,则为null。

        final String vendorKey = "someVendorKey";
        geoFire.getLocation(vendorKey, new LocationCallback() {
            @Override
            public void onLocationResult(String key, GeoLocation location) {
                if (key.compareTo(vendorKey) == 0) {
                    if (location == null) {
                        // vendor has not logged a location yet (or it was removed)
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });