title: Swift/firebase/ ascending number of children in firebase
body: <p>My database screenshot
  <a href="https://i.stack.imgur.com/2BXkh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2BXkh.png" alt="enter image description here"></a></p>
  
  <p>I want to restrict user to input only up to 5 text messages. Therefore I want to add "1","2","3","4","5" ordered children with message and date value. Actually I could achieve it by some kind of manipulation through addition of the following piece of code</p>
  
  <pre><code>        if message0 &gt; 1 &amp;&amp; message0 &lt; 7 {
          message0 = message0 - 1
      }
  </code></pre>
  
  <p>But if I skip this part then when system record data into Firebase it always skips "2" child, and then numerate them in normal ascending way.</p>
  
  <p>Database screenshot</p>
  
  <p><a href="https://i.stack.imgur.com/SZqvz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SZqvz.png" alt="enter image description here"></a></p>
  
  <p>I put below my full code. It works but I feel that above manipulation is kind of strange and might be later bring troubles. If you have idea what I do wrong and some good advice who properly to add number children in ascending way I would highly appreciate.
  Also want to add code which would check existing children to avoid overwriting under the same child number. But have no idea how to do it.
  Anyway thanks for any advice in advance.  </p>
  
  <pre><code>  func sendInputTextToFirebase() { 
          var message0 = MyGlobalVariables.refMessages.child(MyGlobalVariables.uidUser!).observe(.childAdded) { (snapshot) in
              var a = [String]()
              a.append(snapshot.key)
              print(a)
              let b = a.count
              print(b)
                  }
  
      //TODO: make child counting into dbase upto 5 in more elegant way
      if message0 &gt; 1 &amp;&amp; message0 &lt; 7 {
          message0 = message0 - 1
      }
  
      if message0 &lt; 6 {
      let messageString = String(message0)
      let picker = tergetDateTF.text
      let messageT = [
                  "message": messageTV.text! as String,
                  "Date": picker]
      MyGlobalVariables.refMessages.child(MyGlobalVariables.uidUser!).child(messageString).setValue(messageT)
  
          } else {
  
          print("limit exceeded")
          }}}
  </code></pre>