ios - Receive limited query with custom start point -
i'm building basic chat app swift ios firebase realtime database. messages observed limit least 10. now, want implement functionality of loading earlier send messages. i'm trying achieve using function:
let query = threadref.child("messages").queryorderedbykey().querystarting(atvalue: "2").querylimited(tolast: 2)
which returns query:
(/vyhnj3nnqlseexwajatplhikizi1/messages { = ".key"; l = 2; sp = 2; vf = r; })
and should give me data:
query.observesingleevent(of: .value, with: { (snap) in
but limits query , not set start point specific position.
here firebase database structure:
messages -kgzb3_b26cnktdglnd8 date: senderid: sendername: text: -kgzb4qip6_jqdkrwfey -kgzb4ha0kzklzebiaxw -kgzb577klnkohxsqo9w -kgzb5cqivmhrmu019jf
anyone have idea on how implement feature that?
okay found way wanted. first of misunderstood way access data firebase. how query:
let indexvalue = messages.first?.firebasekey let query = messageref.queryorderedbykey().queryending(atvalue:indexvalue).querylimited(tolast: 3)
1) firebase key saved custom chat messages
2) construct query:
- order key
- set ending oldest message
- limit array query desired length
then query used:
query.observesingleevent(of: .value, with: { snapshot in child in snapshot.children.droplast().reversed() { let firesnap = (child as! firdatasnapshot) //do stuff data } })
1) query single event
2) iterate on children , needed droplast() make sure don't have duplicated messages , reverse correct order.
3) cast current child firdatasnapshot access data
since couldn't find simple example thought leave solution here incase other people running same problem.
Comments
Post a Comment