title: How can I update key values in realtime database?
body: <p><img src="https://i.stack.imgur.com/DDlB6.png" alt="data structure" /></p>
  <p>I want to update key values when it removed, so I wrote codes below. These codes are in the Checkbox listener.</p>
  <p>What I did first :</p>
  <pre><code>if(isChecked) {
      Long size = dataSnapshot.child(&quot;Bookmark&quot;).getChildrenCount();
      ref_h.child(userUid).child(&quot;Bookmark&quot;).child(Long.toString(size + 1)).setValue(diningUid);
  } else {
      for (int i = 1; i &lt;= dataSnapshot.child(&quot;Bookmark&quot;).getChildrenCount(); i++) {
          if (dataSnapshot.child(&quot;Bookmark&quot;).child(Integer.toString(i)).getValue().toString().equals(diningUid)) {
              dataSnapshot.child(&quot;Bookmark&quot;).child(Integer.toString(i)).getRef().removeValue();
              isRemoved = true;
          }
          if(isRemoved) {
              if(i != dataSnapshot.child(&quot;Bookmark&quot;).getChildrenCount()){
                  String newDiningUid;
                  newDiningUid = dataSnapshot.child(&quot;Bookmark&quot;).child(Integer.toString(i + 1)).getValue().toString();
                  Log.d(&quot;newUID&quot;, newDiningUid);
                  ref_h.child(userUid).child(&quot;Bookmark&quot;).child(Integer.toString(i)).setValue(newDiningUid);
              } else {
                  dataSnapshot.child(&quot;Bookmark&quot;).child(Integer.toString(i)).getRef().removeValue();
              }
          }
      }
  }
  </code></pre>
  <p>But when the Checkbox status changes rapidly, key values are messed up.
  like this</p>
  <p><a href="https://i.stack.imgur.com/WwtlG.png" rel="nofollow noreferrer">data structure 2</a></p>
  <p>Then I fixed the code with try-catch phrase.</p>
  <pre><code>if(isChecked) {//if not checked before listener runs, add to bookmark
      Long size = dataSnapshot.child(&quot;Bookmark&quot;).getChildrenCount();
      ref_h.child(userUid).child(&quot;Bookmark&quot;).child(Long.toString(size + 1)).setValue(diningUid);
  } else {//if checked before listener runs, remove from bookmark
      for (int i = 1; i &lt;= dataSnapshot.child(&quot;Bookmark&quot;).getChildrenCount(); i++) {
          try {
              if (dataSnapshot.child(&quot;Bookmark&quot;).child(Integer.toString(i)).getValue().toString().equals(diningUid)) {
                  dataSnapshot.child(&quot;Bookmark&quot;).child(Integer.toString(i)).getRef().removeValue();
                  isRemoved = true;
              }
          } catch (NullPointerException e) {
              if (dataSnapshot.child(&quot;Bookmark&quot;).child(Integer.toString(i + 1)).getValue().toString().equals(diningUid)) {
                  dataSnapshot.child(&quot;Bookmark&quot;).child(Integer.toString(i + 1)).getRef().removeValue();
                  isRemoved = true;
              }
          }
          if(isRemoved) {
              //update afterward value's index
              if(i != dataSnapshot.child(&quot;Bookmark&quot;).getChildrenCount()){
                  String newDiningUid;
                  newDiningUid = dataSnapshot.child(&quot;Bookmark&quot;).child(Integer.toString(i + 1)).getValue().toString();
                  ref_h.child(userUid).child(&quot;Bookmark&quot;).child(Integer.toString(i)).setValue(newDiningUid);
              } else {//remove last value
                  try {
                      dataSnapshot.child(&quot;Bookmark&quot;).child(Integer.toString(i)).getRef().removeValue();
                  } catch (NullPointerException e) {
                      dataSnapshot.child(&quot;Bookmark&quot;).child(Integer.toString(i + 1)).getRef().removeValue();
                  }
              }
          }
      }
  }
  </code></pre>
  <p>But I don't think I solved this problem.
  How can I update key values properly?
  or do I need to add a delay to Checkbox?
  or change the data structure?</p>