<!--
{
  "availability" : [
    "iOS: 10.0.0 - 17.0.0",
    "iPadOS: 10.0.0 - 17.0.0",
    "macCatalyst: 13.1.0 - 17.0.0",
    "macOS: 10.12.0 - 14.0.0",
    "visionOS: 1.0.0 - 1.0.0",
    "watchOS: 3.0.0 - 10.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "CloudKit",
  "identifier" : "/documentation/CloudKit/CKDiscoverAllUserIdentitiesOperation",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "CloudKit"
    ],
    "preciseIdentifier" : "c:objc(cs)CKDiscoverAllUserIdentitiesOperation"
  },
  "title" : "CKDiscoverAllUserIdentitiesOperation"
}
-->

# CKDiscoverAllUserIdentitiesOperation

An operation that uses the device’s contacts to search for discoverable iCloud users.

```
class CKDiscoverAllUserIdentitiesOperation
```

## Overview

Use this operation to discover iCloud users that match entries in the device’s Contacts database. CloudKit uses the email addresses and phone numbers in each Contact record to search for a matching iCloud account.

Although your app doesn’t need authorization to use the Contacts database to execute this operation, if it has authorization, you can use the [`contactIdentifiers`](/documentation/CloudKit/CKUserIdentity/contactIdentifiers) property on any returned user identity to fetch the corresponding Contact record from the database.

> Note: This operation scales linearly with the number of email addresses and phone numbers in the device’s Contacts database, and may take some time to complete.

Before CloudKit can return a user’s identity, you must ask for their permission by calling [`requestApplicationPermission(_:completionHandler:)`](/documentation/CloudKit/CKContainer/requestApplicationPermission(_:completionHandler:)). Do this as part of any onboarding where you can highlight the benefits of being discoverable within the context of your app.

The operation executes the handlers you provide on an internal queue it manages. You must provide handlers capable of executing on a background queue. Tasks that need access to the main queue must redirect as appropriate.

The operation calls [`discoverAllUserIdentitiesCompletionBlock`](/documentation/CloudKit/CKDiscoverAllUserIdentitiesOperation/discoverAllUserIdentitiesCompletionBlock) after it executes and returns results. Use the completion handler to perform housekeeping tasks for the operation. It should also manage any failures, whether due to an error or an explicit cancellation.

> Note: Because this class inherits from <doc://com.apple.documentation/documentation/Foundation/Operation>, you can also set the <doc://com.apple.documentation/documentation/Foundation/Operation/completionBlock> property. The operation calls both completion handlers if they’re both set.

CloudKit operations have a default QoS of <doc://com.apple.documentation/documentation/Foundation/QualityOfService/default>. Operations with this service level are discretionary. The system schedules their execution at an optimal time according to battery level and network conditions, among other factors. Use the <doc://com.apple.documentation/documentation/Foundation/Operation/qualityOfService> property to set a more appropriate QoS for the operation.

The following example shows how to create the operation, configure its callbacks, and execute it using the default container’s queue:

```swift
func fetchUserIdentities(
    completion: @escaping (Result<[CKUserIdentity], any Error>) -> Void) {

    var identities = [CKUserIdentity]()

    // Create an operation to discover all the iCloud users
    // in the user's Contacts database that use the app, and
    // opt in to being discoverable.
    let operation = CKDiscoverAllUserIdentitiesOperation()

    // Cache the user identities as CloudKit discovers them.
    operation.userIdentityDiscoveredBlock = { userIdentity in
        identities.append(userIdentity)
    }

    // If the operation fails, return the error to the caller.
    // Otherwise, return the array of discovered user identities.
    operation.discoverAllUserIdentitiesCompletionBlock = { error in
        if let error = error {
            completion(.failure(error))
        } else {
            completion(.success(identities))
        }
    }

    // Set an appropriate QoS and add the operation to the
    // default container's queue to execute it.
    operation.qualityOfService = .userInitiated
    CKContainer.default().add(operation)
}
```

## Topics

### Creating an Operation

[`init()`](/documentation/CloudKit/CKDiscoverAllUserIdentitiesOperation/init())

Creates an operation for searching the device’s contacts.

### Processing the Operation Results

[`userIdentityDiscoveredBlock`](/documentation/CloudKit/CKDiscoverAllUserIdentitiesOperation/userIdentityDiscoveredBlock)

The closure to execute for each user identity.

[`discoverAllUserIdentitiesCompletionBlock`](/documentation/CloudKit/CKDiscoverAllUserIdentitiesOperation/discoverAllUserIdentitiesCompletionBlock)

The closure to execute when the operation finishes.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)