Devesh Agrawal Devesh Agrawal - 3 months ago 30
Android Question

How to delete object in firebase without knowing full url in android

This is my code to add new object in firebase.

private void addProductVariant(String pid, String vid, String mrp, String sellPrice) {
final String uid = getUid();
String key = databaseReference.child("shops-product-list").child(uid).push().getKey();

ShopProductVariant shopProductVariant = new ShopProductVariant(pid, vid, mrp, sellPrice);
Map<String, Object> shopProductVariantValues = shopProductVariant.toMap();

Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/shops-product-list/" + uid + "/"+ key, shopProductVariantValues);

databaseReference.updateChildren(childUpdates);
}


This works fine.

Now i want to delete the same object. i don't know the location (full url). This is code i have written so far.

private void deleteProductVariant(String pid, String vid, String mrp, String sp){

final String uid = getUid();
ShopProductVariant shopProductVariant = new ShopProductVariant(pid, vid, mrp, sp);

//databaseReference.child("shops-product-list").child(uid).child("product-list").setValue(shopProductVariant);

//mDatabase.child("users").child(userId).child("username").setValue(name);
}


How to get key value and delete data object on that location?

Answer

If there is only a single item matching the conditions, I'd recommend keeping the key of that item somewhere. Looking up the same item is both more expensive than needed and likely to become a problem at some point.

But within your current requirements, this should work:

private void deleteProductVariant(String pid, String vid, String mrp, String sp){

  final String uid = getUid();

  DatabaseReference listRef = databaseReference.child("shops-product-list").child(uid).child("product-list");

  listRef. addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
      for (DataSnapshot variantSnapshot: snapshot.getChildren()) {
        ShopProductVariant variant = variantSnapshot.getValue(ShopProductVariant.class);
        if (variant.pid == pid && variant.vid == void) {
          variantSnapshot.getRef().removeValue();
        }
      }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
      throw databaseError.toException();
    }
  })
}

You'll note that this code loads the entire product list for a user to find the item to delete. The reason for this is that you need to check against two values (pid and vid), while Firebase queries can only check against a single condition. If this is a performance problem, you can consider adding a helper property that combines the two values (see Query based on multiple where clauses in firebase).

Since you say the combination of pid and vid is unique for your app, you might even consider using those as the key of your products instead of using a push ID:

String key = pid + "_" + vid;

Deleting then becomes as simple as:

private void deleteProductVariant(String pid, String vid, String mrp, String sp){
  final String uid = getUid();

  DatabaseReference listRef = databaseReference.child("shops-product-list").child(uid).child("product-list");

  DatabaseReference itemRef = listRef.child(pid + "_" + vid);

  itemRef.removeValue();
}
Comments