title: How to add new child in Firebase database Android
body: <p>I'm trying to add a new child using the <code>DatabaseReference</code> in my <code>Firebase</code> Android app. I'm doing: </p>
  
  <pre><code>DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference() 
  mDatabase.child("childToAdd").setValue(1);
  </code></pre>
  
  <p>I can do this with a regular <code>Firebase</code> Reference as it would add the child to the database if it isn't there.<br>
  How could I go about doing this with <code>DatabaseReference</code>? </p>
  
  <p>Edit: Thanks for all the suggestions but I'm having issues with the following code. When it enters the if block it does not push the data onto the database.     </p>
  
  <p><a href="https://gist.github.com/rounaksalim95/a5cba332400c6caf8320f15b0cbf06e8" rel="noreferrer">https://gist.github.com/rounaksalim95/a5cba332400c6caf8320f15b0cbf06e8</a></p>
  
  <p>When I try this with the old Firebase reference I get error code 11 (User code called from firebase runloop) and with the new database reference I get error code -3 (PermissionDenied even though I have no security rules).  </p>
  
  <p>Update:</p>
  
  <p>I got it to do what I wanted to using a single value event listener: </p>
  
  <pre><code>Firebase userRef = new Firebase(BASEURL + "userData");
  
  userRef.addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
          if (dataSnapshot.child(user.getUid()).getValue() == null) {
              userRef.child(user.getUid()).setValue(1);
          }
      }
  
      @Override
      public void onCancelled(FirebaseError firebaseError) {
  
      }
  });  
  </code></pre>
  
  <p>However database reference doesn't let me add values like this. </p>