Pagination Swift 4 FireStore (Post Beta) - PullRequest
       31

Pagination Swift 4 FireStore (Post Beta)

0 голосов
/ 22 февраля 2019

У меня есть CollectionView, где я пытаюсь добавить нумерацию страниц с помощью Firestore.В настоящее время в моей коллекции 13 документов с одной и той же меткой времени.Отметка времени была создана с помощью FireStore FieldValue TimeStamp.

Не получается правильно разбить на страницы данные, основанные на отметке времени.У меня есть гипотеза, что это потому, что все значения являются одинаковыми временными метками, поэтому, когда они сортируют их, они делают это случайным образом и не могут получить их с того места, где остановились.

В моем ViewDidLoad у меня естьСледующее, чтобы настроить мои исходные данные из FireStore:

let db = Firestore.firestore()
        db.collection("Genres").document("Fiction").collection("recentlyAdded").limit(to: 10).order(by: "timeStamp", descending: true).getDocuments { (snapshot, error) in
            if error == nil {
                for document in snapshot!.documents {
                  //  print(document)

                    self.genresArrayInfo.append(document)
                    self.paginateKey = (self.genresArrayInfo.last)?.get("uploadKey") as! String
                }

                self.setUpWritersCollection()
                print(self.genresArrayInfo.count)
                print(self.paginateKey)
            }
        }

Чтобы диктовать положение моей нумерации страниц, у меня есть следующие переменные:

 var genresArrayInfo = [AnyObject]()
    var paginateKey : String = ""
    var isPaginating : Bool = false
In-order to trigger my pagination, I have the following



    func paginate() {

            isPaginating = true
            let db = Firestore.firestore()
            db.collection("Genres").document("Fiction").collection("recentlyAdded").order(by: "timeStamp").start(after: [self.paginateKey]).limit(to: 1).getDocuments { (snapshot, error) in
                if error == nil {
                    for document in snapshot!.documents {
                       // print(document)
                        print(document.get("uploadKey") as! String)
                        self.genresArrayInfo.append(document)

                    }
                    self.paginateKey = (self.genresArrayInfo.last)?.get("uploadKey") as! String
                    self.writersCollection.reloadData()
                    self.writersCollection.reloadInputViews()
                    self.isPaginating = false
                    print(self.genresArrayInfo.count)
                    print(self.paginateKey)
                } else {
                    print(error)
                }

            }//End of DB Query


        }

        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            if scrollView == self.artistsCollection {

                let offsetY = scrollView.contentOffset.y
                let contentHeight = scrollView.contentSize.height

                if (offsetY > contentHeight - scrollView.frame.height - scrollView.frame.height * 0.10) {


                    if !isPaginating { //We've reached the bottom
                        paginate()
                    }

                }
            }
        }

Любые идеи будут с благодарностью.

Я просто пытаюсь понять, как заставить нумерацию страниц работать правильно.

...