1

In my database one section have 10000+ records. I just want query I specific index like the 56th ... 96th. Can I have a way to do that?

    let ref: FIRDatabaseReference = FIRDatabase.database().reference()

    let q = (ref.child("WORLDS").queryOrdered(byChild: "publishTime"))
    q.queryLimited(toLast: 40)
q.observe(.value, with: { (snap) in ...

Like that I just got the last 40 records

AL.
  • 36,815
  • 10
  • 142
  • 281
J. Sue
  • 79
  • 9

1 Answers1

1

Every time you call queryLimited on a location or query, it returns a new query. So you'll need to make sure you keep a reference to that new query:

let ref: FIRDatabaseReference = FIRDatabase.database().reference()

let q = (ref.child("WORLDS").queryOrdered(byChild: "publishTime"))
let q2 = q.queryLimited(toLast: 40)
q2.observe(.value, with: { (snap) in ...

Or just:

let ref: FIRDatabaseReference = FIRDatabase.database().reference()

let query = (ref.child("WORLDS").queryOrdered(byChild: "publishTime")).queryLimited(toLast: 40)
query.observe(.value, with: { (snap) in ...
Ahmad Labeeb
  • 1,056
  • 11
  • 21
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    But how can I query a specific index? Not the last or first. – J. Sue Mar 06 '17 at 17:08
  • You cannot access items by index. It goes against Firebase's realtime nature. But you can use `queryStartingAtValue()` to start at a specific anchor point (typically the last item on the previous page). See my answer here for more on that (and a code snippet): http://stackoverflow.com/questions/27691254/firebase-query-with-limit-offset-possible-solutions/27691931#27691931 – Frank van Puffelen Mar 06 '17 at 17:38