func startHomeFeedLiveUpdaters()

in FriendlyPix/FPFeedViewController.swift [696:720]


  func startHomeFeedLiveUpdaters() {
    // Make sure we listen on each followed people's posts.
    followingRef?.observe(.childAdded, with: { followingSnapshot in
      // Start listening the followed user's posts to populate the home feed.
      let followedUid = followingSnapshot.key
      var followedUserPostsRef: DatabaseQuery = self.database.reference(withPath: "people/\(followedUid)/posts")
      if followingSnapshot.exists() && (followingSnapshot.value is String) {
        followedUserPostsRef = followedUserPostsRef.queryOrderedByKey().queryStarting(atValue: followingSnapshot.value)
      }
      followedUserPostsRef.observe(.childAdded, with: { postSnapshot in
        if postSnapshot.key != followingSnapshot.key {
          let updates = ["/feed/\(self.uid)/\(postSnapshot.key)": true,
                         "/people/\(self.uid)/following/\(followedUid)": postSnapshot.key] as [String: Any]
          self.ref.updateChildValues(updates)
        }
      })
      self.observers.append(followedUserPostsRef)
    })
    // Stop listening to users we unfollow.
    followingRef?.observe(.childRemoved, with: { snapshot in
      // Stop listening the followed user's posts to populate the home feed.
      let followedUserId: String = snapshot.key
      self.database.reference(withPath: "people/\(followedUserId)/posts").removeAllObservers()
    })
  }