title: Firebase - Atomic writes Across Multiple Locations not working
body: <p>How can I make this function to update the realtime database in Firebase? I managed to make the function work but only to write new ID's for posts when updateChild is triggered. How can I make it to update the current posts by their post ID?</p>
  
  <pre><code>   var pathToPictures = [Pictures]()
     func updateAllImagesOfCurrentUserInDatabase() {
  
      //refrences
      let ref = FIRDatabase.database().reference()
      let uid = FIRAuth.auth()?.currentUser?.uid
  
      //match the user ID value stored into posts with current userID and get all the posts of the user
      let update = ref.child("posts").queryOrdered(byChild: "userID").queryEqual(toValue: uid)
      update.observe(FIRDataEventType.value, with: { (snapshot) in
             // print(snapshot)
  
          self.pathToPictures.removeAll()
  
          if snapshot.key != nil {
  
          let results = snapshot.value as! [String : AnyObject]
  
          for (_, value) in results {
              let pathToPostPicture = Pictures()
  
              if let pathToImage = value["pathToImage"] as? String , let postID = value["postID"] as? String {
                  pathToPostPicture.postImageUrl = pathToImage
                  pathToPostPicture.postID = postID
                  self.pathToPictures.append(pathToPostPicture)
                  print("Image and POST ID: \(pathToPostPicture.postImageUrl!)")
                  print("Post ID is : \(postID)")
  
                  if FIRAuth.auth()?.currentUser?.uid == uid {
                  ref.child("Users_Details").child(uid!).child("profileImageUrl").observeSingleEvent(of: .value, with: { (userSnapshot) in
                      print(userSnapshot)
  
                      let userIMageUrl = userSnapshot.value as! String
                      pathToPostPicture.postImageUrl = userIMageUrl
                      self.pathToPictures.append(pathToPostPicture)
                      print("This is the image path:" + userIMageUrl + String(self.pathToPictures.count))
  
                      // Generate the path
                      let newPostRef = ref.child("posts").childByAutoId()
                      let newKey = newPostRef.key as String
                      print(newKey)
                      let updatedUserData = ["posts/\(postID)/pathToImage": pathToPostPicture.postImageUrl]
                      print("This is THE DATA:" , updatedUserData)
                      ref.updateChildValues(updatedUserData as Any as! [AnyHashable : Any])
  
                  })
  
                  }
  
                  print(self.pathToPictures.count)
  
              }
  
              }
          } else {
              print("snapshot is nill")
  
          }
  
          self.collectionView?.reloadData()
  
      })
  }
  </code></pre>
  
  <p>UPDATE: This is how my database looks like</p>
  
  <p>This is my database:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code> "Users_Details" : {
      "aR0nRArjWVOhHbBFB8yUfao64z62" : {
        "profileImageUrl" : "url",
        "userID" : "aR0nRArjWVOhHbBFB8yUfao64z62"
      },
      "oGxXznrS2DS4ic1ejcSfKB5UlIQ2" : {
        "profileImageUrl" : "url",
        "userID" : "oGxXznrS2DS4ic1ejcSfKB5UlIQ2"
      }
    },
    "posts" : {
      "-KlzNLcofTgqJgfTaGN9" : {
        "fullName" : "full name",
        "interval" : 1.496785712879506E9,
        "normalDate" : "Tue, 06 Jun 2017 22:48",
        "pathToImage" : "url",
        "userID" : "oGxXznrS2DS4ic1ejcSfKB5UlIQ2"
      },
      "-KlzNXfvecIwBatXxmGW" : {
        "fullName" : "full name",
        "interval" : 1.496785761349721E9,
        "normalDate" : "Tue, 06 Jun 2017 22:49",
        "pathToImage" : "url",
        "userID" : "oGxXznrS2DS4ic1ejcSfKB5UlIQ2"
      },</code></pre>
  </div>
  </div>
  </p>
  
  <p>Let me tell you what it does: It's looping through the posts and finds all the current user posts. Then it takes the path to image url of every post and assign it to an array. After that it's finding the current user path to image url and updates the whole array with that. Now, I don't know how to update the database with the new values. If anybody knows it will be much appreciate it!</p>
  
  <p>I am looking to make Atomic Writes Across Multiple Locations. Can somebody show me how to fix my code to do that?</p>
  
  <p>It will look something like this:</p>
  
  <pre><code> // ATOMIC UPDATE HERE - IF SOMEBODY CAN FIND THE RIGHT WAY OF DOING THAT
  
                      // Generate a new push ID for the new post
                      let newPostRef = ref.child(byAppendingPath: "posts").childByAutoId()
                      let newPostKey = newPostRef.key
                      // Create the data we want to update
                      let updatedUserData = ["posts/\(newPostKey)": ["pathToImage": self.pathToPictures]] as [String : Any]
  
                      // Do a deep-path update
                      ref.updateChildValues(updatedUserData)
  </code></pre>