title: Firestore Paginating data + Snapshot listener
body: <p>I am working with Firestore right now and have a little bit of a problem with pagination.<br>
  Basically, I have a collection (assume 10 items) where each item has some data and a timestamp.  </p>
  
  <p>Now, I am fetching the first 3 items like this:  </p>
  
  <pre><code>Firestore.firestore()
      .collection("collectionPath")
      .order(by: "timestamp", descending: true)
      .limit(to: 3)
      .addSnapshotListener(snapshotListener())
  </code></pre>
  
  <p>Inside my snapshot listener, I save the last document from the snapshot, in order to use that as a starting point for my next page.  </p>
  
  <p>So, at some time I will request the next page of items like this:</p>
  
  <pre><code>Firestore.firestore()
      .collection("collectionPath")
      .order(by: "timestamp", descending: true)
      .start(afterDocument: lastDocument)
      .limit(to: 3)
      .addSnapshotListener(snapshotListener2()) // Note that this is a new snapshot listener, I don't know how I could reuse the first one
  </code></pre>
  
  <p>Now I have the items from index 0 to index 5 (in total 6) in my frontend. Neat!</p>
  
  <p>If the document at index 4 now updates its timestamp to the newest timestamp of the whole collection, things start to go down.<br>
  Remember that the timestamp determines its position on account of the order clause!  </p>
  
  <p>What I expected to happen was, that after the changes are applied, I still show 6 items (and still ordered by their timestamps)</p>
  
  <p>What happened was, that after the changes are applied, I have only 5 items remaining, since the item that got pushed out of the first snapshot is not added to the second snapshot automatically.  </p>
  
  <p>Am I missing something about Pagination with Firestore?  </p>
  
  <p>EDIT: As requested, I post some more code here:<br>
  This is my function to return a snapshot listener. Well, and the two methods I use to request the first page and then the second page I posted already above</p>
  
  <pre><code>private func snapshotListener() -&gt; FIRQuerySnapshotBlock {
      let index = self.index
      return { querySnapshot, error in
          guard let snap = querySnapshot, error == nil else {
              log.error(error)
              return
          }
  
          // Save the last doc, so we can later use pagination to retrieve further chats
          if snap.count == self.limit {
              self.lastDoc = snap.documents.last
          } else {
              self.lastDoc = nil
          }
  
          let offset = index * self.limit
  
          snap.documentChanges.forEach() { diff in
              switch diff.type {
              case .added:
                  log.debug("added chat at index: \(diff.newIndex), offset: \(offset)")
                  self.tVHandler.dataManager.insert(item: Chat(dictionary: diff.document.data() as NSDictionary), at: IndexPath(row: Int(diff.newIndex) + offset, section: 0), in: nil)
  
              case .removed:
                  log.debug("deleted chat at index: \(diff.oldIndex), offset: \(offset)")
                  self.tVHandler.dataManager.remove(itemAt: IndexPath(row: Int(diff.oldIndex) + offset, section: 0), in: nil)
  
              case .modified:
                  if diff.oldIndex == diff.newIndex {
                      log.debug("updated chat at index: \(diff.oldIndex), offset: \(offset)")
                      self.tVHandler.dataManager.update(item: Chat(dictionary: diff.document.data() as NSDictionary), at: IndexPath(row: Int(diff.oldIndex) + offset, section: 0), in: nil)
                  } else {
                      log.debug("moved chat at index: \(diff.oldIndex), offset: \(offset) to index: \(diff.newIndex), offset: \(offset)")
                      self.tVHandler.dataManager.move(item: Chat(dictionary: diff.document.data() as NSDictionary), from: IndexPath(row: Int(diff.oldIndex) + offset, section: 0), to: IndexPath(row: Int(diff.newIndex) + offset, section: 0), in: nil)
                  }
              }
          }
          self.tableView?.reloadData()
      }
  }
  </code></pre>
  
  <p>So again, I am asking if I can have one snapshot listener that listens for changes in more than one page I requested from Firestore</p>