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?