0

DELETE/ SELECT * FROM Product WHERE code = typedValue AND userId = currentUser

firebase look like this

I have this but this is like SELECT * FROM Product WHERE code = typedValue


ref = FirebaseDatabase.getInstance().getReference();
String currentUser = FirebaseAuth.getInstance().getCurrentUser().getUid();
query = ref.child("Product").orderByChild("code").equalTo(typedValue);

query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        if(snapshot.hasChildren()){
            for(DataSnapshot codeSnapshot: snapshot.getChildren()){
                codeSnapshot.getRef().removeValue();
            }
            Toast.makeText(AbcActivity.this, "Delete",Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(AbcActivity.this, "This product is not available",Toast.LENGTH_SHORT).show();
        }
    }

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Radud
  • 1
  • Firebase doesn't support DELETE/UPDATE queries, where you send both the condition and the action to the server. You'll need to execute the query in your application code to determine the affected node(s) and then update each of them individually, as the code you shared seems to be doing. What's the problem with the code you shared? – Frank van Puffelen Mar 24 '21 at 18:01
  • @FrankvanPuffelen The code works, but different users may have the product with the same code. So I can remove a product with the same code added by another user – Radud Mar 24 '21 at 18:11
  • Ah, so does this boil down to: how can I perform a query on two fields? Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. For an example of this and other approaches, see my answer here: https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase. The only alternative is to perform the additional filter in your application code, so checking the UID inside the loop in `onDataChange`. – Frank van Puffelen Mar 24 '21 at 18:37
  • @FrankvanPuffelen thank you very much. – Radud Mar 24 '21 at 19:07

0 Answers0