0

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Devesh Agrawal
  • 8,982
  • 16
  • 82
  • 131
  • If you don't know the `key` of the item to delete, how will you identify which of the child items to delete? Or do you want to delete all child items with the same `pid`? – Frank van Puffelen Aug 07 '16 at 16:53
  • @FrankvanPuffelen, i want to delete only single item with same pid and vid pass as function args. – Devesh Agrawal Aug 07 '16 at 17:26

1 Answers1

1

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();
}
Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807