0

Database layout is

root
  posts
    -postId_1
       -userId
       -other data
    -postId_2
       -userId
       -other data

When a user wants to scroll through all posts (location irrelevant) I paginate based on postId using:

let postsRef = Database.database().reference().child("posts")

if startKey == nil {

    postsRef.queryOrderedByKey().queryLimited(toLast: 20).observeSingleEvent(of: .value) { [weak self](snapshot) in

        guard let firstChild = snapshot.children.allObjects.first as? DataSnapshot else { return }

        let arr = snapshot.children.allObjects as! [DataSnapshot]
        for child in arr.reversed() {

            let postId = child.key
            guard let dict = child.value as? [String:Any] else { return }
            // create a post, append to datasource ...
        }

        self?.startKey = firstChild.key
    }

else {

    postsRef.queryOrderedByKey().queryEnding(atValue: startKey!).queryLimited(toLast: 21).observeSingleEvent(of: .value) { (snapshot) in

        // same as above ...
    }
}

When I want to query other users based on their location in proximity to the current user I use:

let radius = (10 * 2) * 1609.344 // this is 10 miles

guard let currentLocation = locationManager.location else { return }
        
let lat = currentLocation.coordinate.latitude
let lon = currentLocation.coordinate.longitude
let location = CLLocation(latitude: lat, longitude: lon)

let region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: radius, longitudinalMeters: radius)

let geofireRef = Database.database().reference().child("geo")
let geoFire = GeoFire(firebaseRef: geofireRef)

regionQuery = geoFire.query(with: region)
queryHandle = regionQuery?.observe(.keyEntered, with: { [weak self](key: String!, location: CLLocation!) in

    let userId = key
    let userLocation = location

    self?.arrOfUserIds.append(userId)
}

regionQuery?.observeReady({ [weak self] in

    self?.paginateBasedOnUserIdReturnedFromRegionQuery()

}

Everything above works fine. The problem is when the regionQuery?.observeReady is called, I have an array of all the userIds in the surrounding 10 mile radius. That can be 1 user or 1000 users and each user can have 1 post or 100+ posts. I can't figure out how to paginate the posts ref based on userId.

Each post has a userId but in the first example above I'm paginating based on postId. I want to paginate the posts ref based on the userIds returned from the regionQuery like

func paginateBasedOnUserIdReturnedFromRegionQuery() {

    for userId in arrOfUserIds {

        // how to paginate the posts ref using the userId ???
    }
}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
  • "I can't figure out how to paginate the posts" With what? – El Tomato Jul 18 '21 at 05:00
  • using the userId. After I get all of the userIds from the regionQuery, I only want to paginate the posts from those users, not all users. The way the database is laid out the posts ref is based on only the postId so every user's post is pulled – Lance Samaria Jul 18 '21 at 05:03
  • When they talk about paging records, they usually use the table view or collection view. And you are using something else and put all records in the scroll view or something? – El Tomato Jul 18 '21 at 05:06
  • In the first example above, when I paginate I pull 20 posts and 21 posts. After I create I post I append it to a collectionView, that works fine, I have zero problems. As I fetch the posts from Firebase I just reverse the array, so the posts are in reverse order from most recent to furthest. But those posts are from very user. When I get the userIds from the regionQuery that is a subset of users that I want to paginate on. Those users can have n amount of posts. I only want to paginate the posts ref based on their userIds – Lance Samaria Jul 18 '21 at 05:10
  • I'm not sure I understand your goal here. Are you trying to add an additional filter (on user id) to GeoFire? – Frank van Puffelen Jul 18 '21 at 05:21
  • @FrankvanPuffelen after I get the userIds from the regionQuery, how do I use their userId to paginate on the posts ref. – Lance Samaria Jul 18 '21 at 05:31
  • Why are you using Firebase Database if you want to support paging with the collection view? – El Tomato Jul 18 '21 at 06:57
  • @ElTomato why not use it? I haven't seen anything that said that it's incorrect to use for paging. – Lance Samaria Jul 18 '21 at 08:35
  • Firebase Realtime Database is a wrong animal to use for paging. Firestore is built for paging. Compare the documentation. There is no section for paging if you read the doc for Firebase Realtime Database. – El Tomato Jul 18 '21 at 09:01
  • @ElTomato I wish I knew that a long time. All of my apps use it for paging. It's not difficult but of curse in a situation like I am in for my question that isn't anything that I can do. I have to pull all of the users posts as I loop through each userId. As far as Firestore, OI never learned because I was always focused on RTB. In the future I will have to look into it. Thanks! – Lance Samaria Jul 18 '21 at 09:42
  • Under this topic (https://stackoverflow.com/questions/56640389/firebase-realtime-database-pagination-showing-20-records-at-a-time), I used Firebase Realtime Database for paging two years ago. It wasn't a pleansant memory for sure. – El Tomato Jul 18 '21 at 10:00
  • @ElTomato I paginate in a different manner. You use `willDisplayCell`, I use the scrollView method `scrollViewDidEndDragging` and page based off the s datasource count. It works perfectly fine for me. I even use it with full screen video scrolling like tik tok and I have no problems. – Lance Samaria Jul 18 '21 at 10:31

0 Answers0