Home > OS >  How to add several identical child in a Firebase database
How to add several identical child in a Firebase database

Time:10-26

I have a mobile game with several systems to have money (transaction or via an offer wall).

Players can choose between 3 types of "Gold coins" pack, and therefore obviously that they can request the same pack several times if they have the necessary number of pieces.

But I have a problem in my code:

public void mediumPack(String emailMP) {
        
    Date date = Calendar.getInstance().getTime();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);

     Calendar calendar = Calendar.getInstance();
     calendar.setTime(date);
     Date previousDate = calendar.getTime();

     String dateString = dateFormat.format(previousDate);
        
      HashMap<String, Object> map = new HashMap<>();
        map.put("amount", 5000);
        map.put("email", emailMP);
        map.put("date", dateString);
        map.put("status", MPStatus);
        
        DatabaseReference referenceMP = FirebaseDatabase.getInstance().getReference().child("demandMP").child(user.getUid());
        
        referenceMP.child("Medium Pack")
            .setValue(map)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {

                    if (task.isSuccessful()) {

                        Toast.makeText(MediumPackActivity.this, "Success", Toast.LENGTH_SHORT).show();
                        
                        finish();

                    } else {
                        Toast.makeText(MediumPackActivity.this, "Error: "   task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    }

                }
        });
}
getMP.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                    
            String emailMP = emailMediumPack.getText().toString();
                    
                if (TextUtils.isEmpty(emailMP)) {
     
                   emailMediumPack.setError("Invalid put mail");
                   return;
            }
                
            mediumPack(emailMP);
        }
});

So the child "demandMP" and uid are already created when creating an account.

In the uid I want to add the referenceMP, everything works but the problem is that after trying several times a single reference can be created (if I use another email address it will only modify the email in the "Medium Pack"

Is there a way to create multiple references?

Otherwise, I thought of creating a random ticket instead of the name "Medium Pack", considering this possibility someone could tell me how to create a random ticket that looks like this:

MP-XX...(18 chars)-5000

CodePudding user response:

If you want generate unique child nodes under Medium Pack every time the code runs, call push() like this:

DatabaseReference referenceMP = FirebaseDatabase.getInstance().getReference().child("demandMP").child(user.getUid());

referenceMP.child("Medium Pack")
    .push() //            
  • Related