Firebase queries support pagination, but are slightly different from what you're used to. Instead of working with offsets, Firebase uses what are called snap values ββto determine where to start.
Getting elements for the first page is easy, you just specify a limit on the number of elements to retrieve:
ref = Database.database().reference() query = ref.child("club").queryOrderedByKey().limitToFirst(10) query.observe(DataEventType.value, with: { (snapshot) in
Now in the block, track the key of the last element that you specified to the user:
for obj in array { let snapshot:DataSnapshot = obj as! DataSnapshot if let childSnapshot = snapshot.value as? [String: AnyObject] { lastKey = childSnapshot.key if let clubName = childSnapshot["name"] as? String { print(clubName) } } }
Then, to get the next page, create a query that starts with the last key you saw:
query = ref.child("club").queryOrderedByKey().startAt(lastKey).limitToFirst(11)
You will need to extract another element than the size of your page, since the anchor element is extracted on both pages.
source share