title: Firebase Data flickers on addition of new values
body: <p>I am making a social app to which I am fetching some data and flushing it to the collection view. I am flushing the all the posts from firebase to the posts array. I am also fetching the user information that posted the specific image. Both the database are 2 different models. Following is my data model :</p>
  
  <pre><code>posts
    |- &lt;post_id&gt;
           |- caption
           |- ImageURL
           |- views
           |- spot
               |- spot_id
                    |- sender&lt;user_id&gt;
                    |- spotted(value)
                    |- timestamp
           |- author(&lt;user_id&gt;)
  
  users
    |- &lt;user_id&gt;
           |- name
  </code></pre>
  
  <p>Following is the way I am fetching the post data in collectionVC and storing all to posts array:</p>
  
  <pre><code>func initialiseAllPostsContent(){
      FBDataservice.ds.REF_CURR_USER.child("connections/following").observe(.childAdded) { (snapshot) in
          if let snapshot = snapshot.value as? String {
              self.followerKeys.append(snapshot)
          }
      }
      if uid != nil {
          self.followerKeys.append(uid!)
      }
      FBDataservice.ds.REF_POSTS.queryOrdered(byChild: "timestamp").observe(.childAdded, with: { (snapshot) in
          print("post key is ", snapshot.key)
          if let postDict = snapshot.value as? Dictionary&lt;String, Any&gt; {
              let key = snapshot.key
              if let postAuthor = postDict["author"] as? String {
                  for user in self.followerKeys {
                      if postAuthor == user {
                          let post = Posts(postId: key, postData: postDict)
                          self.posts.append(post)
                      }
                  }
              }
          }
      })
      reloadCollectionViewData()
  }
  
  func reloadCollectionViewData() {
      FBDataservice.ds.REF_POSTS.queryOrdered(byChild: "timestamp").observe(.value) { (snapshot) in
          self.collectionView.reloadData()
      }
  }
  
  //I am updating the views on the post after a method is successfull. As soon as this is called, and then if like is pressed, views flicker
  func updateViews(postid: String, views: Int) {
      let viewref = FBDataservice.ds.REF_POSTS.child(postid)
      let newviews = views + 1
      viewref.updateChildValues(["views":newviews])
  }
  
  // fetching the user data from the post data
  
  func getAllPosts(pid: String, completion: @escaping ((String) -&gt; ())) {
      FBDataservice.ds.REF_POSTS.child(pid).observeSingleEvent(of: .value) { (snapshot) in
          if let snapshot = snapshot.value as? Dictionary&lt;String, Any&gt; {
              if let userid = snapshot["author"] as? String {
                  completion(userid)
              }
          }
      }
  }
  
  func getpostAuthorData(authorId : String, completion: @escaping (User) -&gt; ()) {
      FBDataservice.ds.REF_USERS.child(authorId).observeSingleEvent(of: .value) { (snapshot) in
          if let snapshot = snapshot.value as? Dictionary&lt;String, Any&gt; {
              if let userCredential = snapshot["credentials"] as? Dictionary&lt;String, Any&gt; {
                  completion(User(userid: authorId, userData: userCredential))
              }
          }
      }
  }
  </code></pre>
  
  <p>This is how I am assigning data in my <code>cellForItemAtIndexPath</code></p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      self.posts.sort(by: { $0.timestamp &lt; $1.timestamp})
      let post = posts[indexPath.row]
      if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? SpotGroundCell {
          cell.configureCellData(post: post)
          getAllPosts(pid: post.postId) { (userid) in
              self.getpostAuthorData(authorId: userid, completion: { (userdata) in
                  cell.configUserData(user: userdata)
              })
          }
      return cell
      } else {
          return SpotGroundCell()
      }
  
  }
  </code></pre>
  
  <p>The code in my cell : </p>
  
  <pre><code>//Consider this as likes. I allow users to like multiple times. Once the model is loaded, it fetches all the spots according to the timestamp and then siplayer the most recent ones. Even this is doesn't display according to the current image and flickers. I replicate previous cell values even though I am refreshing the view.
  var currentUserSpots = [Spot]() {
      didSet {
          self.currentUserSpots.sort(by: { $0.timestamp &lt; $1.timestamp})
          if !self.currentUserSpots.isEmpty {
              self.emotionImage.image = UIImage(named: (self.currentUserSpots.first?.spotted)!)
              self.emotionImage.alpha = 1
          } else {
              self.emotionImage.image = UIImage(named: "none")
              self.emotionImage.alpha = 0.5
          }
      }
  }
  
  func configUserData(user: User) {
      self.user = user
      self.name.text = self.user.name
  }
  
  func configureCellData(post: Posts) {
      print("Config is now called")
      self.posts = post
      self.caption.text = posts.caption
  
      FBDataservice.ds.REF_POSTS.child(post.postId).child("spot").queryOrdered(byChild: "senderID").queryEqual(toValue: uid!).observeSingleEvent(of: .childAdded) { (snapshot) in
          if let spotData = snapshot.value as? Dictionary&lt;String, Any&gt; {
              let spot = Spot(id: snapshot.key, spotData: spotData)
              if spot.spotted != nil {
                  self.currentUserSpots.append(spot)
              }
          }
      }
  }
  </code></pre>
  
  <p>Now whenever I am making a change or an event which updates the database(like updating a view). I see a flicker in the user object entities(such as name etc). That event also kills other processes and Notification Observers. </p>
  
  <p>I scrapped the internet for the solutions, but by far just was able to <a href="https://stackoverflow.com/q/41640063/4306200">find one</a>, which doesn't solve my problem. </p>
  
  <p>Any help will be greatly appreciated. I am really not sure where am I going wrong. </p>