Swift iOS: Firebase Paging -
i have firebase data:
i want query posts
data through pagination. code converting js code swift code
let postsref = self.rootdatabasereference.child("development/posts") postsref.queryorderedbychild("createdat").querystartingatvalue((page - 1) * count).querylimitedtofirst(uint(count)).observesingleeventoftype(.value, withblock: { snapshot in .... })
when accessing, data page: 1, count: 1
. can data "posts.a" when try access page: 2, count: 1
returns still "posts.a"
what missing here?
assuming or using childbyautoid()
when pushing data firebase, can use queryorderedbykey()
order data chronologically. doc here.
the unique key based on timestamp, list items automatically ordered chronologically.
to start on specific key, have append query querystartingatvalue(_:)
.
sample usage:
var count = numberofitemsperpage var query ref.queryorderedbykey() if startkey != nil { query = query.querystartingatvalue(startkey) count += 1 } query.querylimitedtofirst(uint(count)).observesingleeventoftype(.value, withblock: { snapshot in guard var children = snapshot.children.allobjects as? [firdatasnapshot] else { // handle error return } if startkey != nil && !children.isempty { children.removefirst() } // children })
Comments
Post a Comment