title: Pagination infinitely calling in TableView - Swift/Xcode
body: <p>I have a TableView that displays documents from my Firebase Firestore server. Instead of downloading ALL of the documents on the server, I paginate and download more documents as the user scrolls down the TableView. This works great if there are a lot of documents on the server. If there are only a few documents, however, meaning there's not enough for the user to need to scroll through, then the pagination process is infinitely called, infinitely querying documents from the server. How can I fix this? I know it's an issue with the following line in tableView willDisplayForRowAt:</p>
  <pre><code>if (indexPath.row == fileArray.count - 1)
  </code></pre>
  <p>I just don't know how to fix it.</p>
  <pre><code>struct FileIdentifierStruct {
      var fileName = String()
      var fileDate = String()
  }
  
  var fileArray = [FileIdentifierStruct]()
  
  func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
          // Trigger pagination when scrolled to last cell
          if (indexPath.row == fileArray.count - 1) {
              print(&quot;Calling paginate&quot;)
              paginate()
          }
      }
  
  func paginate() {
          print(&quot;paginate function was called&quot;)
          // This is the main pagination code
          query = query.start(afterDocument: documents.last!)
          // For some reason calling getData here enters an infinite loop until the view is exited
          getData()
      }
  
  func getData() {
          print(&quot;getData was called&quot;)
          query.getDocuments() { (querySnapshot, err) in
              if let err = err {
                  print(&quot;Error getting documents: \(err.localizedDescription)&quot;)
                  let alert = SCLAlertView()
                  alert.showError(&quot;ERROR&quot;, subTitle: &quot;The following error occured while trying to retrieve the documents: \(err.localizedDescription)&quot;)
              } else {
                  querySnapshot!.documents.forEach({ (document) in
  //                    let data = document.data() as [String: AnyObject]
                      
                      // Set up the data modal here
                      let date = document.get(&quot;Date&quot;) as? String ?? &quot;&quot;
                      let id = document.documentID
                      let fileItem = FileIdentifierStruct(fileName: id, fileDate: date)
                      self.fileArray += [fileItem]
                      self.documents += [document]
                  })
                  self.filesTableView.reloadData()
                  self.filesTableView.showDefault()
              }
          }
      }
  </code></pre>