1

I have a table view that displays users ordered by their created date in descending order (newest member is sorted first). In that same table view, I also want to find and show all male users in new york between the ages of 30 and 40 that is based on user settings.

My database looks as follows

{
    "Ka8xxTgyFB8yYKH50j" : {
        "created" : 1583258308.2321649,
        "data" : "male_newyork_30"
    },
    "K222xTgyFBF33FD50j" : {
        "created" : 1583358308.2321649,
        "data" : "male_newyork_35" 
    },
    "Ka32T23R328yYKH50j" : {
        "created" : 1584258308.2321649,
        "data" : "male_seattle_30"  
    },
    "F323F32FB8yYKH50j" : {
        "created" : 1583558308.2321649,
        "data" : "female_newyork_30"
    }
...
}

As referenced in Advanced Firebase Query with filter in Swift, I tried the following with a limit to last 6 users (so it doesn't download everything):

usersRef.queryOrderedByChild("data").queryStartingAtValue("male_newyork_30")
        .queryEndingAtValue("male_newyork_40").queryLimited(toLast: 6).observeSingleEventOfType(
       .Value, withBlock: { snapshot in
    print(snapshot)
})

However, I cannot paginate with this data because I don't know what to keep track of to load the next 6 users.

Normally when I add pagination, I'd query by user created date and keep track of the last user's created date to compare it to the next batch of data. But this approach only paginates and doesn't filter data.

How can I paginate and filter my data? Any help is very appreciated!

Joan
  • 905
  • 2
  • 9
  • 15
  • To get the next "page" of results, you pass in the value of `data` of the last item on page one, and its key (in case there are duplicate values). There are a lot of [questions about Firebase pagination](https://stackoverflow.com/search?q=%5Bfirebase-realtime-database%5D+pagination) already, so I'd also recommend checking those out. – Frank van Puffelen Mar 08 '20 at 23:26
  • Hi @FrankvanPuffelen if I pass in the value of data, then I can't paginate/order by date in firebase. I've switched to firestore for better paginating with multiple orders and queries. Can you take a look at [my most recent question](https://stackoverflow.com/questions/60818665/firestore-order-and-limit-data-doesnt-return-full-result)? Thank you! – Joan Mar 24 '20 at 18:51

0 Answers0