I have a CollectionView
which displays items ordered by location, and I am trying to paginate them by a given amount (f.e 20 items).
I am using Geofire
to order them by location, but the problem is that it doesn´t support queries limited by result count, so I can´t get a whole page without accesing all the ids.
I came up with the idea of geting on the first page all the item keys within the give location ordered by location:
locationQuery?.observe(.keyEntered, with: { (key, location) in
print("KEY: ", key, " and location: ", location.coordinate)
itemKeys.append(key)
})
locationQuery?.observeReady {
print("All item keys loaded. There are: ", itemKeys.count, " items in radius")
locationQuery?.removeAllObservers()
completion(itemKeys) //itemKeys.count would be the number of cells in the collectionView
}
Then save them, and in each page the next (pageAmount) of the itemKeys is retrieved and queried using Firebase rererence.child(itemKeyInArray)
.
My main concern about this is that if I have a very big set of items in database, how efficient would it be iterating (and sorting) through all the keys.
Another approach was starting with a small radius, and increasing it in each page until the limit, and despite it seems more efficient I would still need the total number of items within the radius (to calculate the number of cells in the CollectionView
)
Which would it be the proper approach to paginate the results by location?
There's no way to do pagination on Geofire at the moment. The most common approach I've seen is to do a few progressively wider range queries, so that the nearby items show up first, as you also tried already. You can also get all the keys for a wider range, and then order them on distance yourself to do the pagination. That way you only need to retrieve the details for a subset of the keys. Both approaches are valid, neither is objectively better than the other.
Also see: