1

I currently have a firebase database with a lot of posts in it. The posts are displayed in a UITableViewController in custom cells.

The problem is that it loads all of the cells at once - this can take a lot of time when more posts are coming.

How can I make it load only 10 posts at a time, and load 10 more when scrolling to the bottom?

This is how I fetch my posts:

func startObersvingDB() {
        FIRDatabase.database().reference().child("feed-items").observeEventType(.Value, withBlock: { (snapshot: FIRDataSnapshot) in
            var newUpdates = [Sweet]()



            for update in snapshot.children {
                let updateObject = Sweet(snapshot: update as! FIRDataSnapshot)
                newUpdates.append(updateObject)

            }

            self.updates = newUpdates.reverse()
            self.tableView.reloadData()


        }) { (error: NSError) in
            print(error.description)
        }
    }
D. Finna
  • 467
  • 2
  • 7
  • 19

1 Answers1

0

Consider this example for getting paging to work with Firebase: Swift iOS: Firebase Paging

And for loading more when scrolling, try this in your viewcontroller (Swift3):

extension MyViewController:UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offset = scrollView.contentOffset.y
    if offset > self.tableView.contentSize.height - 50 { //This value is adjustable
        functionToPullMoreDatabaseItems()
    }
}
}
Community
  • 1
  • 1
rmooney
  • 6,123
  • 3
  • 29
  • 29
  • This may be a very dumb question, but what has the height of the contentsize to do with loading more posts? :) – D. Finna Nov 15 '16 at 17:16
  • When the contentOffset.y equals the contentSize.height, that is when you scrolled to the bottom. Subtracting 50 allows you to call the function to load more from the database before you reach the bottom so the data is there by the time you get to it. – rmooney Nov 15 '16 at 19:26