Some Google Cloud APIs return lists of resources that are too
large to fit in a single response. These APIs implement pagination as described
in the [AIP-158 pagination specification](https://google.aip.dev/158) on
google.aip.dev. The service returns a single page of results along with a
`nextPageToken` value that the client can include in subsequent requests to
fetch the next page.

The Google Cloud client libraries for Swift simplify working with
paginated RPCs by providing `AsyncSequence` helpers that handle page tokens and
fetch subsequent pages as you iterate over the results. This document shows you
how to use both automatic iteration and manual page-by-page fetching.

## Before you begin

This document uses the Secret Manager API. To enable this API, follow
the
[Secret Manager quickstart](https://docs.cloud.google.com/secret-manager/docs/create-secret-quickstart.md.txt).

For complete setup instructions for the Swift client libraries, see
[Quickstart: Get started with Swift](https://docs.cloud.google.com/swift/quickstart.md.txt).

## Iterate over items automatically

To handle paginated results in Swift, iterate over the items using a `for try
await` loop. The client library fetches subsequent pages on demand as you
iterate. To iterate over items automatically, follow these steps:

1. Add the imports required to use the client library:


   ```swift
   import Foundation
   import GoogleGax
   import GoogleCloudSecretManagerV1
   ```

   <br />

2. Define a function that accepts the project ID as a parameter:


   ```swift
   func iterateItems(projectId: String) async throws {
   ```

   <br />

3. Initialize the client with the default options:


   ```swift
   let client = try SecretManagerServiceClient()
   ```

   <br />

4. Construct a `ListSecretsRequest` (optionally setting `pageSize`), request
   an asynchronous sequence of items using the
   `METHODByItems` helper method (in this example,
   `listSecretsByItems(request:)`), and iterate over the results:


   ```swift
   let request = ListSecretsRequest().with {
     $0.parent = "projects/\(projectId)"
     $0.pageSize = 25
   }
   let secrets = client.listSecretsByItems(request: request)
   for try await secret in secrets {
     print("Secret name: \(secret.name)")
   }
   ```

   <br />

## Iterate page by page manually

If your app needs direct access to individual pages and page tokens---for example,
when implementing your own pagination layer in a web service---you can call the
standard request method to receive the raw page response. To manage page tokens
yourself, follow these steps:

1. Create a function that initializes the client:


   ```swift
   func iteratePages(projectId: String) async throws {
     let client = try SecretManagerServiceClient()
   ```

   <br />

2. Make paginated requests using the `listSecrets(request:)` method, inspect
   the items on each page, and use the returned `nextPageToken` value to fetch
   subsequent pages:


   ```swift
   var request = ListSecretsRequest().with {
     $0.parent = "projects/\(projectId)"
     $0.pageSize = 25
   }
   while true {
     let response = try await client.listSecrets(request: request)
     for secret in response.secrets {
       print("Secret name: \(secret.name)")
     }
     if response.nextPageToken.isEmpty {
       break
     }
     request.pageToken = response.nextPageToken
   }
   ```

   <br />

## Complete code

The following example shows the complete code for this document:

```swift
import Foundation
import GoogleGax
import GoogleCloudSecretManagerV1

func iterateItems(projectId: String) async throws {
  let client = try SecretManagerServiceClient()
  let request = ListSecretsRequest().with {
    $0.parent = "projects/\(projectId)"
    $0.pageSize = 25
  }
  let secrets = client.listSecretsByItems(request: request)
  for try await secret in secrets {
    print("Secret name: \(secret.name)")
  }
}

func iteratePages(projectId: String) async throws {
  let client = try SecretManagerServiceClient()
  var request = ListSecretsRequest().with {
    $0.parent = "projects/\(projectId)"
    $0.pageSize = 25
  }
  while true {
    let response = try await client.listSecrets(request: request)
    for secret in response.secrets {
      print("Secret name: \(secret.name)")
    }
    if response.nextPageToken.isEmpty {
      break
    }
    request.pageToken = response.nextPageToken
  }
}
```

## What's next

- Learn how to use [long-running operations](https://docs.cloud.google.com/swift/long-running-operations.md.txt).
- Learn how to [override the default authentication
  credentials](https://docs.cloud.google.com/swift/override-credentials).
- Learn how to [override the default endpoint](https://docs.cloud.google.com/swift/override-endpoint.md.txt).
- Learn how to [override the default retry
  policies](https://docs.cloud.google.com/swift/override-retry-policy).
- Learn how to [override the default polling
  policies](https://docs.cloud.google.com/swift/override-polling-policy).
- Learn how to [enable logging](https://docs.cloud.google.com/swift/enable-logging.md.txt) to troubleshoot requests and responses.
- Learn how to configure [package traits](https://docs.cloud.google.com/swift/package-traits.md.txt) for large client libraries.
- Browse the [Swift Cloud client libraries](https://docs.cloud.google.com/swift/reference.md.txt) reference documentation.