Search code examples
androidfirebasegeofire

How do I remove a key completely from Database


I want to completely remove a vehicle from geoFire, but when I run this:

@Override 
    public void onKeyExited(String key) { 
        // Remove any old marker 
        Marker marker = this.markers.get(key); 
        if (marker != null) { 
            marker.remove(); 
            this.markers.remove(key); 
        } 
    } 

It is removed from the current view, but when I refresh the location, the vehicle is there.


Solution

  • Your current code just removes the marker from the app, it does not remove any data from the database. To remove a vehicle, you'd do the opposite of what you do when add a vehicle.

    From the GeoFire for Java documentation on setting location data:

    To set a location for a key simply call the setLocation() method. The method is passed a key as a string and the location as a GeoLocation object containing the location's latitude and longitude:

    geoFire.setLocation("firebase-hq", new GeoLocation(37.7853889, -122.4056973));
    

    To remove a location and delete it from the database simply pass the location's key to removeLocation:

    geoFire.removeLocation("firebase-hq");