<!--
{
  "availability" : [
    "iOS: 11.3.0 -",
    "iPadOS: 11.3.0 -",
    "macCatalyst: 11.3.0 -",
    "macOS: 11.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "ClassKit",
  "identifier" : "/documentation/ClassKit/CLSDataStore/contexts(matchingIdentifierPath:completion:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "ClassKit"
    ],
    "preciseIdentifier" : "c:objc(cs)CLSDataStore(im)contextsMatchingIdentifierPath:completion:"
  },
  "title" : "contexts(matchingIdentifierPath:completion:)"
}
-->

# contexts(matchingIdentifierPath:completion:)

Fetches all the contexts along a given identifier path.

```
func contexts(matchingIdentifierPath identifierPath: [String], completion: @escaping @Sendable ([CLSContext], (any Error)?) -> Void)
```

## Parameters

`completion`

A closure that the method calls with the matching contexts and an optional error that indicates the reason for failure, if any.

## Discussion

> Important:
> You can call this method from synchronous code using a completion handler, as shown on this page, or you can call it as an asynchronous method that has the following declaration:
> 
> ```swift
> func contexts(matchingIdentifierPath identifierPath: [String]) async throws -> [CLSContext]
> ```
> 
> For information about concurrency and asynchronous code in Swift, see <doc://com.apple.documentation/documentation/Swift/calling-objective-c-apis-asynchronously>.

Use this method to get all of the named contexts along the identifier path. For example:

```swift
let store = CLSDataStore.shared
store.contexts(matchingIdentifierPath: ["game", "level-1"]) { contexts, _ in
    for context in contexts {
        print(context.identifier)
    }
}
// Prints "game" and then "level-1"
```

The identifier path is taken starting from [`mainAppContext`](/documentation/ClassKit/CLSDataStore/mainAppContext), which lies at the root of your app’s context hiearchy. For each context that doesn’t exist along the path, the data store calls its delegate’s [`createContext(forIdentifier:parentContext:parentIdentifierPath:)`](/documentation/ClassKit/CLSDataStoreDelegate/createContext(forIdentifier:parentContext:parentIdentifierPath:)) method to create the context. If contexts are missing and the data store has no delegate, the returned list of contexts is incomplete.

> Important:
> ClassKit calls your completion handler on an arbitrary thread. If you need to do work on a particular thread from inside the handler, dispatch that work to the appropriate queue.

---

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)