title: Retrieve only 5 users at a time :Firebase [like Instagram]
body: <p>For the past few days, I have been trying to create an Instagram-like feed for my app. To be more specific: load new posts (5) each time the user updates the feed from the bottom.</p>
  
  <p>I am currently using Firebase to both store and display my data.</p>
  
  <p>My code so far, looks like this:</p>
  
  <pre><code>    var ref:FIRDatabaseReference!
  
      var dict = [String:Any]()
      var posts = [[String:Any]]()
  
     override func viewDidLoad() {
          super.viewDidLoad()
  
          tableView.delegate = self
          tableView.dataSource = self
  
          ref = FIRDatabase.database().reference()
  
          // Do any additional setup after loading the view, typically from a nib.
      }
  
      override func viewDidAppear(animated: Bool) {
  
          loadValues()
  
      }
  
      func loadValues() {
  
          dict.removeAll()
          posts.removeAll()
  
  
          ref.child("posts").queryOrderedByChild("timeCreated").queryLimitedToLast(5).observeEventType(.ChildAdded) { (snapshot:FIRDataSnapshot) in
  
              if let timeCreated = snapshot.value!["timeCreated"] as? Int {
                  self.dict["timeCreated"] = timeCreated
              }
  
              if let postText = snapshot.value!["postText"] as? String {
                  self.dict["postText"] = postText
              }
  
  
              self.posts.append(self.dict)
  
  
              self.tableView.reloadData()
  
          }
  
  
  
  
      }
  
  
      func scrollViewDidScroll(scrollView: UIScrollView) {
  
          if (scrollView.contentOffset.y + scrollView.frame.size.height) &gt;= scrollView.contentSize.height {
  
              //tableView.tableFooterView!.hidden = true
              let pagingSpinner = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
              pagingSpinner.startAnimating()
              pagingSpinner.hidesWhenStopped = true
              pagingSpinner.sizeToFit()
              tableView.tableFooterView = pagingSpinner
  
              //loadMore(5)
  
          } else {
  
              let pagingSpinner = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
              pagingSpinner.stopAnimating()
              pagingSpinner.hidesWhenStopped = true
              pagingSpinner.sizeToFit()
              pagingSpinner.hidden = true
              tableView.tableFooterView = pagingSpinner
              tableView.tableFooterView?.hidden = true
  
  
          }
  
  
      }
  
  
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
  
          return posts.count
  
      }
  
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
  
          let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
  
          if let postText = posts[indexPath.row]["postText"] as? String {
              cell.textLabel!.text = postText
          }
  
          return cell
  
      }
  
      func loadMore(increment:Int) {
  
          //What should go in here?
  
      }
  </code></pre>
  
  <p>So what I am trying to do here - is that I am detecting when the user has scrolled to the bottom (in my scrollViewDidScroll function. Then I am sinply displaying the activity indicator, and calling the function loadMore(5) where 5 is the amount of new posts that I want to display.</p>
  
  <p>So here I have two problems. The timeCreated variable is simply a timestamp, where I have ten records (1-10, where 10 is the newest, and 1 is the oldest). With this code that I have now, the tableView displays the data in an ascending view, starting at 5 and ending at 10. </p>
  
  <p>I have tried to reverse the array of dictionaries (post) by simply doing a .reverse() before appending the dict in the loadValues function. as I simply want it to display 10 at the top, and 5 at the bottom.</p>
  
  <p>The second problem I have is that I can't really seem to find a good and effective way of updating the tableView (adding another 5 records). I have tried to simply just have a global variable with the default value of 5, and then on loadMore simply plus it by five, and then do a removeAll() on both the dict and posts - with no luck (the tableView scrolls to the top, which I don't want to). I have also tried to play with both queryLimitedTolast and queryLimitedToFirst where I ended up duplicating some data. </p>
  
  <p>So in other words, I also need to check that the user can in fact load 5 new unique posts (or ex. 3, if there's only 3 unique posts left).</p>
  
  <p>Does anyone have any ideas on how I would approach this?</p>
  
  <p>Help would be greatly appreciated, as I have struggled with this for the past two days now.</p>