---
title: "contexts(matching:completion:)"
framework: classkit
role: symbol
role_heading: Instance Method
path: "classkit/clsdatastore/contexts(matching:completion:)"
---

# contexts(matching:completion:)

Fetches all the contexts matching a predicate.

## Declaration

```swift
func contexts(matching predicate: NSPredicate, completion: @escaping @Sendable ([CLSContext], (any Error)?) -> Void)
```

```swift
func contexts(matching predicate: NSPredicate) async throws -> [CLSContext]
```

## Parameters

- `predicate`: A predicate that the method uses to search for contexts.
- `completion`: A closure that the method calls with the array of contexts that match the predicate and an optional error that indicates the reason for failure, if any.

## Discussion

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: func contexts(matching predicate: NSPredicate) async throws -> [CLSContext] For information about concurrency and asynchronous code in Swift, see Calling Objective-C APIs Asynchronously. Use the predicate keys defined in CLSPredicateKeyPath to create a predicate that you pass to this method to search for contexts matching certain criteria. For example, to print the titles of all the children of the main app context: let store = CLSDataStore.shared let predicate = NSPredicate(format: "%K = %@", CLSPredicateKeyPath.parent as CVarArg,                                                store.mainAppContext) store.contexts(matching: predicate) { contexts, _ in     for context in contexts {         print(context.title)     } } 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.

## See Also

### Finding contexts that match criteria

- [contexts(matchingIdentifierPath:completion:)](classkit/clsdatastore/contexts(matchingidentifierpath:completion:).md)
- [CLSPredicateKeyPath](classkit/clspredicatekeypath.md)
