title: Showing posts from those users you follow - swift & firebase
body: <p>I have made a following system where users can follow each other but it is not very useful at the moment. I cannot seem to get my head around how to only show the posts of the users you follow - so I am here to ask you how you would do it.</p>
  
  <p>Let me just show you how the database is set up:</p>
  
  <p><a href="https://i.stack.imgur.com/Ult6R.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ult6R.png" alt="enter image description here"></a></p>
  
  <p>As you can see I have a tap called <code>Users</code> - in there all the users ID are stored. Whenever a user wants to follow another user, the tap <code>Following</code> is made and under there the user that the current user pressed follow - his/her ID is store under <code>Following</code>. And then the current users ID is added to the user he/she follows under <code>Followers</code>. Pretty simple but complicated to explain :D I hope you get where I'm going.</p>
  
  <p>So now I want only to show posts of the users I am following. How would you do that? Would you do it like this: When a user is making a post it is added to his own name under a tap called <code>Posts</code> AND added to all the users names under <code>PostsToFollow</code>? I just think it would take up a lot of space in the database? Isn't there a better way to do this?</p>
  
  <p>Well here is a part of my code that is getting me all of the current users followers. How can I use that code to, whenever I am creating a new post, add that to their <code>postsToFollow</code>?</p>
  
  <pre><code>func startObersvingDB() {
          FIRDatabase.database().reference().child("Users").child(IDgotten).child("Followers").observeEventType(.Value, withBlock: { (snapshot: FIRDataSnapshot) in
              var newUpdates = [FollowersStruct]()
  
  
  
              for update in snapshot.children {
                  print(update)
                  let updateObject = FollowersStruct(snapshot: update as! FIRDataSnapshot)
                  newUpdates.append(updateObject)
  
              }
  
              self.updates = newUpdates.reverse()
              self.tableView.reloadData()
  
  
          }) { (error: NSError) in
              print(error.description)
          }
      }
  </code></pre>
  
  <p><strong>To Frank</strong>:</p>
  
  <p>I've made this for adding the entire post to all the users that are following my user. I cannot do that because I have a like and comment button on the posts so the likes on one users posts will not be the same as other users posts and that is why I think the idea of adding the key to the post in the followers walls is great. But how do I do that?</p>
  
  <p>I have made this so far, trying to accomplish what you said :-) :</p>
  
  <pre><code>    let feed = Sweet(content: update, addedByUser: name!, profilePhoto: uid, likesForPost: ["user id": false], date: timePosted, category: 1, workoutComment: "", workoutTime: "")
      let feedRef = FIRDatabase.database().reference().child("feed-items").childByAutoId()
  
  
      feedRef.setValue(feed.toAnyObject())
  
      FIRDatabase.database().reference().child("Users").child(uid).child("Followers").observeEventType(.Value, withBlock: { (snapshot: FIRDataSnapshot) in
          var newFollowers = [FollowersStruct]()
  
  
  
          for updateFollowers in snapshot.children {
              let updateObjectFollowers = FollowersStruct(snapshot: updateFollowers as! FIRDataSnapshot)
              newFollowers.append(updateObjectFollowers)
  
          }
  
          self.followings = newFollowers
  
  
          print(self.followings.count)
  
  
  
  
          for i in 0..&lt;self.followings.count {
              let followers = self.followings[i]
  
              let feedRefFollowers = FIRDatabase.database().reference().child("Users").child(followers.Id).child("Wall").childByAutoId()
              feedRefFollowers.setValue(feed.toAnyObject())
          }
  
  
      }) { (error: NSError) in
          print(error.description)
      }
  </code></pre>