---
title: Real-Time Audiences for Feature Experimentation segment qualification methods for the Swift SDK
slug: feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-swift
docTags: 
createdAt: 2026-08-05T20:40:01.000Z
---

## Prerequisites

You must enable and configure [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8) before fetching qualified segments and checking if the user is qualified for the given audience segment.

## fetchQualifiedSegments - asynchronous

### Minimum SDK version

4.0.0

### Description

Fetches all qualified audience segments for the user context.

`fetchQualifiedSegments` is a method of the `UserContext` object. See [OptimizelyUserContext](docId\:lx6nIl7h8TioF9Dj8FS5t) for details.

### Parameters

The following table describes the parameters for the `fetchQualifiedSegments` method:

| Parameter            | Type                       | Description                                                |
| -------------------- | -------------------------- | ---------------------------------------------------------- |
| `options `(optional) | \[OptimizelySegmentOption] | A set of options for fetching qualified segments from ODP. |
| `completionHandler`  | (OptimizelyError?) -> Void | A completion handler to be called with the fetch result.   |

The SDK provides an `async` form, `fetchQualifiedSegments(options:) async throws`. This form throws errors instead of passing them to a completion handler.

:::hint{type="warning"}
In version 6.0.0, the `async` form ignores the `options` parameter. To pass segment options, use the completion handler form or the synchronous form.
:::

### Returns

The completion handler to be called on completion that updates the qualified segments array.

- On success, it passes a nil error.
- On failure, it passes one of these errors:
  - `OptimizelyError.invalidSegmentIdentifier`
  - `OptimizelyError.fetchSegmentsFailed`.

:::hint{type="info"}
You can read and write directly to the qualified segments array to bypass the remote fetching process from ODP or use your own fetching service. This can be helpful when testing or debugging.
:::

### Example fetchQualifiedSegments – asynchronous

The following is an example of calling the `fetchQualifiedSegments` method and accessing the returned completion object:

```swift
let attributes: [String: Any] = ["app_version": "1.3.2"]
let user = optimizelyClient.createUserContext(userId: "fs-id-123", attributes: attributes)
    
// Without segment option.
Task {
   let attributes: [String: Any] = ["app_version": "1.3.2"]
   let user = optimizely.createUserContext(userId: "fs-id-123", attributes: attributes)
   try? await user.fetchQualifiedSegments()
}
```

## fetchQualifiedSegments – synchronous

### Minimum SDK version

4.0.0

### Description

This is a synchronous version of the `fetchQualifiedSegments` asynchronous version above.

:::hint{type="info"}
This call blocks the calling thread until fetching is completed.
:::

### Parameters

The following table describes the parameters for the `fetchQualifiedSegments` method:

| Parameter              | Type                       | Description                                                |
| ---------------------- | -------------------------- | ---------------------------------------------------------- |
| \`options\` (optional) | \[OptimizelySegmentOption] | A set of options for fetching qualified segments from ODP. |

### Returns

This method does not provide a return value. If it fails, it throws one of the following errors:

- `OptimizelyError.invalidSegmentIdentifier`
- `OptimizelyError.fetchSegmentsFailed(String)`

### Example fetchQualifiedSegments - synchronous

The following is an example of calling the `fetchQualifiedSegments` method and accessing the returned completion object:

```swift
let optimizely = OptimizelyClient(sdkKey: <Your_SDK_Key>)

optimizely.start { result in
   let attributes: [String: Any] = ["app_version": "1.3.2"]
	 let user = optimizely.createUserContext(userId: "fs-id-123", attributes: attributes)
                  
	 do {
      // this blocks the calling thread until fetch is completed. 
      try user.fetchQualifiedSegments()

      let decision = user.decide(key: "flag1")
   } catch OptimizelyError.invalidSegmentIdentifier {
			print("[AudienceSegments] audience segments fetch failed (user id is not registered yet or invalid)")
   } catch {
			print("[AudienceSegments] \(error)")
   }
}
```

The following diagram shows the network calls between your application, the Feature Experimentation Swift SDK, and the ODP server when calling `fetchQualifiedSegments`:

![](https://api.archbee.com/api/optimize/BrbvVORKJ5iIeSMZye5kI/vQFbMhV9TaG-XYvxvKLN4_26d1783d2323930a63136114f2988c14d51476114b5019e802d3281c82a93350-image.png)

1. Your application calls the `fetchQualifiedSegments` method.
2. The Feature Experimentation Swift SDK makes a GraphQL call to ODP to fetch segments the user ID qualifies for.
3. ODP responds with the qualified audience segments.
4. The Swift SDK automatically caches the fetched segments mapping the user ID to the qualified segments.
5. The `fetchQualifiedSegments` method completes without an error. The SDK saves the fetched segments in the `qualifiedSegments` property of the user context.
6. Call the `decide` method to get the flag decision for a given user, a decision object that contains the flag's status (enabled or disabled), and the assigned variation key.

The Swift SDK fetches the segments and then caches them. This means that if you are requesting the segments again (when new user contexts are created), you can retrieve the audience segment information from the cache rather than from the remote ODP server.

The cache is used for the `fetchQualifiedSegments` call. This method is called on the user context (the user context is fixed, including the real-time audiences the user qualifies for).

The cache *only* applies when calling the `fetchQualifiedSegments` call. If you set the cache timeout (`segmentsCacheTimeoutInSecs`) to `0`, cached segments never expire. To disable the cache, set the cache size (`segmentsCacheSize`) to `0`. Optimizely uses the least recently used (LRU) algorithm, so the oldest record is bumped out when the maximum size is reached. If there is a cache miss upon the method call, Optimizely makes a network request.

You can add the following options to your `odpSegmentOptions` array to bypass caching:

- `ignoreCache` – Bypass segments cache for lookup and save.
- `resetCache` – Reset all segments cache.

## isQualifiedFor

### Minimum SDK version

4.0.0

### Description

Check if the user qualifies for the given audience segment.

### Parameters

The following table describes the parameters for the `isQualifiedFor` method:

| Parameter | Type   | Description                                                  |
| --------- | ------ | ------------------------------------------------------------ |
| `segment` | String | The ODP audience segment name to check if the user qualifies |

### Returns

`true` if the user is qualified.

### Examples

The following is an example of whether the user qualifies for an ODP segment:

```swift
let attributes: [String: Any] = ["laptop_os": "mac"]
let user = optimizelyClient.createUserContext(userId: "user123", attributes: attributes)

user.fetchQualifiedSegments { error in
   let isQualified = user.isQualifiedFor(segment: "segment1")
}
```

## Source files

The language and platform source files containing the implementation for Swift are available on [Github](https://github.com/optimizely/swift-sdk/blob/master/Sources/Optimizely/OptimizelyClient.swift).
