首页 > 解决方案 > 如果没有创建 Geofire,我如何检查 Geofire 以注销用户?

问题描述

问题总结:我是Geofire的Android Studio和Firebase新手,请温柔。我的目标是当用户在我的应用程序的地图屏幕上没有 Geofire 位置时将其注销。我不知道如何在我的 Geofire 中查询“g”。如何查询“g”子项以查看它是否已创建?如果未创建“g”,则需要注销它们。所以目标是查询“g”并创建方法,如果“g”为空,则将用户注销。

我做过的事情:很多 Stackoverflow 帖子都没有使用 Android Studio。我确实发现我已经在 firebase 中使用 geofire 插入了位置,但它并没有解决我的问题。我也试过谷歌火力检查孩子是否存在

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,我希望用户退出,但现在用户没有退出。

如何查询 g

标签: androidfirebase-realtime-databasegeofire

解决方案


这是一个调用示例,getLocation以查看供应商是否注册了位置。

来自 javadocs 的LocationCallback.onLocationResult

使用键的当前位置调用此方法。如果 GeoFire 中没有存储密钥的位置,则 location 将为空。

        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) {

            }
        });

推荐阅读