void ReadDataPaginateQuery()

in firestore/android/FirestoreSnippetsCpp/app/src/main/cpp/snippets.cpp [1086:1123]


void ReadDataPaginateQuery(firebase::firestore::Firestore* db) {
  using firebase::Future;
  using firebase::firestore::DocumentSnapshot;
  using firebase::firestore::Error;
  using firebase::firestore::Query;
  using firebase::firestore::QuerySnapshot;

  // Paginate queries by combining query cursors with the Limit() method. For
  // example, use the last document in a batch as the start of a cursor for the
  // next batch.

  // [START paginate]
  // Construct query for first 25 cities, ordered by population
  Query first = db->Collection("cities").OrderBy("population").Limit(25);

  first.Get().OnCompletion([db](const Future<QuerySnapshot>& future) {
    if (future.error() != Error::kErrorOk) {
      // Handle error...
      return;
    }

    // Get the last visible document
    const QuerySnapshot& document_snapshots = *future.result();
    std::vector<DocumentSnapshot> documents = document_snapshots.documents();
    const DocumentSnapshot& last_visible = documents.back();

    // Construct a new query starting at this document,
    // get the next 25 cities.
    Query next = db->Collection("cities")
                     .OrderBy("population")
                     .StartAfter(last_visible)
                     .Limit(25);

    // Use the query for pagination
    // ...
  });
  // [END paginate]
}