title: Prevent duplicate data entries in Firebase?
body: <p>I have an application that allows a user to upload an image of a car and some details about the car. Now I want to make sure that only one entry can be made for each car model, so I want to run a method to check if a CarModel is existing, and if it is stop the user from uploading. 
  How do I go about this? </p>
  
  <p>Here's my code to upload the image and car details:</p>
  
  <pre><code>    // Creating UploadImageFileToFirebaseStorage method to upload image on storage.
  public void UploadImageFileToFirebaseStorage() {
  
      // Checking whether FilePathUri Is empty or not.
      if (FilePathUri != null) {
  
          // Setting progressDialog Title.
          progressDialog.setTitle("Image is Uploading...");
  
          // Showing progressDialog.
          progressDialog.show();
  
          // Creating second StorageReference.
          StorageReference storageReference2nd = storageReference.child(Storage_Path + System.currentTimeMillis() + "." + GetFileExtension(FilePathUri));
  
          // Adding addOnSuccessListener to second StorageReference.
          storageReference2nd.putFile(FilePathUri)
                  .addOnSuccessListener(new OnSuccessListener&lt;UploadTask.TaskSnapshot&gt;() {
                      @Override
                      public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
  
                          // Getting image name from EditText and store into string variable.
                          String CarMake = carMake.getText().toString().trim();
                          String CarModel = carModel.getText().toString().trim();
  
                          // Hiding the progressDialog after done uploading.
                          progressDialog.dismiss();
  
                          // Showing toast message after done uploading.
                          Toast.makeText(getApplicationContext(), "Image Uploaded Successfully ", Toast.LENGTH_LONG).show();
  
                          @SuppressWarnings("VisibleForTests")
                          ImageUploadInfo imageUploadInfo = new ImageUploadInfo(taskSnapshot.getDownloadUrl().toString(), CarMake, CarModel);
  
                          // Getting image upload ID.
                          String ImageUploadId = databaseReference.push().getKey();
  
                          // Adding image upload id s child element into databaseReference.
                          databaseReference.child(ImageUploadId).setValue(imageUploadInfo);
                      }
                  })
                  // If something goes wrong .
                  .addOnFailureListener(new OnFailureListener() {
                      @Override
                      public void onFailure(@NonNull Exception exception) {
  
                          // Hiding the progressDialog.
                          progressDialog.dismiss();
  
                          // Showing exception erro message.
                          Toast.makeText(uploadActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
                      }
                  })
  
                  // On progress change upload time.
                  .addOnProgressListener(new OnProgressListener&lt;UploadTask.TaskSnapshot&gt;() {
                      @Override
                      public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
  
                          // Setting progressDialog Title.
                          progressDialog.setTitle("Image is Uploading...");
  
                      }
                  });
  
  
      }
      else {
  
          Toast.makeText(uploadActivity.this, "Please Select Image or Enter Car Details", Toast.LENGTH_LONG).show();
  
      }
  }
  </code></pre>
  
  <p>And I have another method which checks for duplicates:</p>
  
  <pre><code>public void duplicateCheck(){
  
      databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(final DataSnapshot dataSnapshot) {
              for (DataSnapshot data : dataSnapshot.getChildren()) {
                  //If email exists then toast shows else store the data on new key
                  if (!data.getValue(Car.class).getCarModel().equals(carModel)) {
                      databaseReference.child(databaseReference.push().getKey()).setValue(new Car(carMake, carModel));
                  } else {
                      Toast.makeText(uploadActivity.this, "Car Model Already Exists.", Toast.LENGTH_SHORT).show();
                  }
              }
          }
  
          @Override
          public void onCancelled(final DatabaseError databaseError) {
          }
      });
  }
  </code></pre>
  
  <p>And I call both of these methods on an upload button:</p>
  
  <pre><code>// Adding click listener to Upload image button.
      UploadButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
  
              // Calling method to upload selected image on Firebase storage.
              UploadImageFileToFirebaseStorage();
              duplicateCheck();
  
          }
      });
  </code></pre>