Google Professional Cloud Developer Question 456
Select 3Google Cloud PlatformYou are developing a RESTful API on Google Cloud that retrieves a list of user data from Firestore. Due to the large volume of data, you need to implement pagination to ensure efficient data retrieval and avoid timeouts. Which of the following steps should you take to implement pagination correctly?
- A
Use the
limit()function to specify the maximum number of documents to return in each page. - B
Use the
startAfter()function with a reference to the last document from the previous page to retrieve the next set of results. - C
Hard-code a fixed offset value to skip documents for each page.
- D
Include a cursor or token with each paginated response for the client to use in subsequent requests.
- E
Set a global timeout for all queries to prevent long-running requests instead of implementing pagination.
Show answer and explanation
Correct answers: A, B, D
Explanation
To implement pagination in a robust and efficient manner on Firestore, you need to use the limit() function to control the number of results per page and the startAfter() function to query subsequent pages based on the last retrieved document. Additionally, providing a cursor or token in the response allows the client to seamlessly continue pagination. Using offsets or setting a global timeout are not recommended approaches for handling large datasets in Firestore.
- A. Correct.
Correct. The
limit()function is essential for defining the number of results to return per page. - B. Correct.
Correct. The
startAfter()function allows you to specify where the query should begin, based on the last document from the previous page. - C. Incorrect.
Incorrect. Using a fixed offset is inefficient with Firestore as it requires skipping records, which can lead to performance issues.
- D. Correct.
Correct. Including a cursor or token in the response allows the client to continue fetching results from where the previous query ended.
- E. Incorrect.
Incorrect. Setting a global timeout does not address the need for pagination; it only limits the execution time.