java - 如何从Firebase数据库中删除Geofire key -JAVA

标签 java android firebase geofire

我正在用Android开发应用程序,基本上它的作用是为您提供打开开关的选项,并在应用程序中打开服务,将您的位置数据记录到Firebase数据库中的Key。我已经做到了。

当用户打开开关时,它将创建一个UUID并将信息放入该节点中。并且所有这些都工作正常,但是我正在努力删除该密钥。

我已经尝试了所有内容,并查看了所有其他帖子,但似乎没有任何效果。

我已经设法在这样的服务中创建UUID

static String uniqueID = UUID.randomUUID().toString();

并像这样将位置信息保存到该密钥中


    LocationCallback mLocationRequestCallback = new LocationCallback(){
        @Override
        public void onLocationResult(LocationResult locationResult) {
            for(Location location : locationResult.getLocations()){
                mLastLocation = location;
                DatabaseReference userRef = FirebaseDatabase.getInstance().getReference().child("usersAvailable").child("Users");

                GeoFire geoFire = new GeoFire(userRef);
                geoFire.setLocation(uniqueID, new GeoLocation(mLastLocation.getLongitude(), mLastLocation.getLatitude()), new GeoFire.CompletionListener() {
                    @Override
                    public void onComplete(String key, DatabaseError error) {
                        Log.d(TAG,"Succesfully added "+uniqueID);
                    }
                });

            }
        }
    };



这样,一旦服务停止,我就会尝试删除密钥。

    @Override
    public void onDestroy() {
        super.onDestroy();
        mFusedLocationClient.removeLocationUpdates(mLocationRequestCallback);
        wakeLock.release();
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("userAvailable").child("Users");
        ref.child(uniqueID).removeValue();

      ref.addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
              if(dataSnapshot.hasChild(uniqueID)) {
                  dataSnapshot.getRef().removeValue();
              }
          }

          @Override
          public void onCancelled(@NonNull DatabaseError databaseError) {

          }
      });

        GeoFire geoFire = new GeoFire(ref);
        geoFire.removeLocation(uniqueID, new GeoFire.CompletionListener() {
            @Override
            public void onComplete(String key, DatabaseError error) {
                Log.d(TAG, "Removed Location Successfully: "+uniqueID);
                Log.e(TAG,key+"\n"+error);
            }
        });
    }



这是我得到的日志,似乎可以运行,removeLocation,但仍然无法删除任何内容

D/GeoJobService: Removed Location Successfully: 63825afd-7710-4cd7-b799-7be049281862
E/GeoJobService: 63825afd-7710-4cd7-b799-7be049281862
    null


我还尝试通过对密钥进行硬编码来删除密钥,但它仍然不能删除数据。我删除密钥的唯一方法是进入数据库并手动删除每个密钥。

编辑

这是数据库

这些键与上述内容不同,因为我手动删除了这些键

Database

最佳答案

您有错字,请更改以下内容:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("userAvailable").child("Users");


到这个:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("usersAvailable").child("Users");


usersAvailables应该与您的数据库相同。

关于java - 如何从Firebase数据库中删除Geofire key -JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56339761/

相关文章:

java.sql.SQLException : Bad connection URL while accessing the database

ios - 如何识别用户是否打开了 App store 的链接,但当时并没有安装该应用程序?

javascript - 类型错误 : Object(…) is not a function

ios - iOS的FCM静默推送通知结构?

java - 类型转换在编译器/机器级别做什么?

java - 斯坦福解析器(类型化依赖)异常

java - 递归函数——保存ArrayList Java

java - 非法状态异常 : Could not execute method for android:onClick when trying to migrate to another page?

android - 如何在来自多个用例/交互器的 View 模型中使用 Kotlin Flows 的组合结果?

Android 结构化查找和替换 XML