<!--
{
  "availability" : [
    "iOS: 10.0.0 -",
    "iPadOS: 10.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.12.0 -",
    "tvOS: 10.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 3.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreData",
  "identifier" : "/documentation/CoreData/NSPersistentContainer/performBackgroundTask(_:)-39sch",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Core Data"
    ],
    "preciseIdentifier" : "c:objc(cs)NSPersistentContainer(im)performBackgroundTask:"
  },
  "title" : "performBackgroundTask(_:)"
}
-->

# performBackgroundTask(_:)

Executes a closure on a private queue using an ephemeral managed object context.

```
func performBackgroundTask(_ block: @escaping @Sendable (NSManagedObjectContext) -> Void)
```

## Parameters

`block`

A closure that is executed by the persistent container against a newly created private context. The private context is passed into the block as part of the execution of the block.

## Discussion

Each time this method is invoked, the persistent container creates a new [`NSManagedObjectContext`](/documentation/CoreData/NSManagedObjectContext) with the [`concurrencyType`](/documentation/CoreData/NSManagedObjectContext/concurrencyType-swift.property) set to [`NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType`](/documentation/CoreData/NSManagedObjectContextConcurrencyType/privateQueueConcurrencyType). The persistent container then executes the passed in block against that newly created context on the context’s private 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)