Contents

encode(_:)

Serializes the hierarchy handle into a Mach port reference and accompanying data.

Declaration

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 layer hierarchy handle. The port serves as a reference to the layer hierarchy that you can share with another process. Use the port and data together to reconstruct the handle using init(port:data:).

handle.encode { port, data in
    guard port != MACH_PORT_NULL else {
        // The handle 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)
}

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:).

See Also

Sharing a layer hierarchy handle using Mach