---
title: "addCompletedHandler(_:)"
framework: metal
role: symbol
role_heading: Instance Method
path: "metal/mtlcommandbuffer/addcompletedhandler(_:)"
---

# addCompletedHandler(_:)

Registers a completion handler the GPU device calls immediately after the GPU finishes running the commands in the command buffer.

## Declaration

```swift
func addCompletedHandler(_ block: @escaping MTLCommandBufferHandler)
```

## Parameters

- `block`: A Swift closure or an Objective-C block that Metal calls after the GPU finishes running the commands in the command buffer.

## Mentioned in

Converting a GPU’s counter data into a readable format Preparing your Metal app to run in the background Setting up a command structure

## Discussion

Discussion You can register one or more completion handlers for the same command buffer. The GPU device’s driver (on the CPU) calls the completion handlers after the GPU finishes executing the command buffer. important: You can only call this method before calling the command buffer’s commit() method. For example, you can use the command buffer’s gpuEndTime and gpuStartTime properties to calculate how much time the GPU spends running the command buffer. The completion handler is also a good place to check the status property to determine whether the GPU successfully completes the buffer’s commands. If the status is equal to MTLCommandBufferStatus.error, you can investigate further by checking the error and log properties for more details about the issue. See Command buffer debugging for more methods and properties that can help you isolate the issue. warning: Avoid calling the insertDebugCaptureBoundary() method within the completion handler, which can cause a debug-time deadlock if you request GPU frame capture.

## See Also

### Registering state change handlers

- [addScheduledHandler(_:)](metal/mtlcommandbuffer/addscheduledhandler(_:).md)
- [MTLCommandBufferHandler](metal/mtlcommandbufferhandler.md)
