Chris Comas
Chris Comas

Reputation: 216

Firestore Querying with pagination- Swift

this code does not work together. I have to query either: .whereField("Privacy", isEqualTo: "Public") or:

     let myQuery: Query
        if lastDocument == nil {
            myQuery = firestoreRef.order(by: "Date", descending: false).limit(to: 8)
        } else {
            myQuery = firestoreRef.start(afterDocument: lastDocument).limit(to: 5) } 

Its not letting me query them together and I don't understand why, is there a work-around for this? Im basically Paginating my data so when the user reaches the bottom of the screen It calls this function. if this function was run once before (which it was in "ViewDidLoad") it grabs the last document. Hence, my next query will start after "lastDocument". However the main thing is that it's not letting me query both of these ^^^ phrases together.

var lastDocument: Documentsnapshot!



  func loadPosts () {
        let firestoreRef = Firestore.firestore().collection("Posts")
            .whereField("Date", isGreaterThanOrEqualTo: Date() - 10800)
            .whereField("Privacy", isEqualTo: "Public")

        fetchingmore = true
        let myQuery: Query
        if lastDocument == nil {
            myQuery = firestoreRef.order(by: "Date", descending: false).limit(to: 8)
        } else {
            myQuery = firestoreRef.start(afterDocument: lastDocument).limit(to: 5) }

            myQuery.getDocuments { (snapshot, error) in
       .
       .
       .
       etc... }

Upvotes: 0

Views: 206

Answers (1)

Chris Comas
Chris Comas

Reputation: 216

I figured it out, it's because I was using the .getDocuments call instead of the .addsnapshotListener call. However, If anyone knows How I can accomplish the same thing whilst keeping the .getDocuments call, i'm open ears!

Upvotes: 1

Related Questions