<!--
{
  "availability" : [
    "iOS: 26.0.0 -",
    "iPadOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "BrowserEngineKit",
  "identifier" : "/documentation/BrowserEngineKit/LayerHierarchyHostingTransactionCoordinator/encode(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "BrowserEngineKit"
    ],
    "preciseIdentifier" : "c:objc(cs)BELayerHierarchyHostingTransactionCoordinator(im)encodeWithBlock:"
  },
  "title" : "encode(_:)"
}
-->

# encode(_:)

Serializes the transaction coordinator into a Mach port reference and accompanying data.

```
func encode(_ block: (mach_port_t, Data) -> Void)
```

## Parameters

`block`

A closure that receives a Mach port and serialized data as arguments.

## Discussion

This method invokes the supplied closure with a `mach_port_t` and `Data` object that together represent the serialized transaction coordinator. The port serves as a reference to the coordinator that you can share with another process. Use the port and data together to reconstruct the coordinator using [`init(port:data:)`](/documentation/BrowserEngineKit/LayerHierarchyHostingTransactionCoordinator/init(port:data:)).

```swift
coordinator.encode { port, data in
    guard port != MACH_PORT_NULL else {
        // The coordinator is already invalidated.
        return
    }
    
    // Send the port and data to the rendering process.
    sendToRenderingExtension(port: port, data: data)
    
    // Dispose of the port after sending.
    mach_port_deallocate(mach_task_self(), port)
}
```

> Important: Dispose of the Mach port in the supplied closure after sending it to the other process. The port is `MACH_PORT_NULL` if the coordinator has already been invalidated.

Each call to this method creates a new port reference and unique serialized data. Decode each port and data pair only once using [`init(port:data:)`](/documentation/BrowserEngineKit/LayerHierarchyHostingTransactionCoordinator/init(port:data:)).

---

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)