title: Firebase Swift Pagination on News Feed
body: <p>I'm creating an instagram style feed and trying to figure out how to paginate posts based on people that you follow.</p>
  
  <p>My data is structured as follows: </p>
  
  <pre><code> posts:
        user1:
             post1
             post2
        user2:
             post3
             post4
        user3: 
             post5
             post6
             post7
  
   user-following:
        user1:
             user2
             user3
  </code></pre>
  
  <p>So posts are organized by the user that created the post. I also have a structure for people that the current user follows. So i grab the uid's of the users that are followed by the current user, then go to the post section and get all of the posts from those users, and each post has a createdAt value. </p>
  
  <p>When i use queryByChild("createdAt"), it sorts the data per user, not absolutely. 
  So it sorts user2's posts, then user3's posts, instead of doing an absolute sort since its pulling the data in chunks. </p>
  
  <p>I need the most recent overall posts independent of the users that own the posts for the news feed. i can get all the post info in a snapshot, but is there a way to then sort that snapshot? I need to find a way to get the database to sort the information as a whole, not in chunks, then pull the most recent 'x' amount of posts from that sorted list.</p>
  
  <p>Otherwise i have to pull everything into my program, then sort it in an array, then pull the most recent 'x' amount of posts from the array for pagination. problem is if there are 1000 posts, that's a massive data pull that would make the app super slow and possibly crash. any suggestions?</p>