Thanks Quinn for your very informative answer.
Your code does work on my side as well, and is what I would generally use. However it has a pain point for me - I don't see how I'm able to send various kinds of Codable conforming typed messages over the XPC connection.
That forced me into using XPCDictionary.
How can one XPCSession transfer let's say two distinct types of Codable messages?
struct EndpointCheckinMessage: Codable {
var endpoint: XPCEndpoint
var endpointBundleID: String
}
struct SpecificClientMessage: Codable {
var title: String
var description: String
}
How would one XPCSession send both of these messages efficiently to the daemon? Because my solution was initially wrap these into a single struct:
struct GeneralClientMessage: Codable {
var kind: MessageKind // EndpointCheckinMessage, SpecificClientMessage, etc.
var body: Data // This would contain the above types encoded with `JSONEncoder`.
}
And that's where I hit this issue and had to resort to XPCDictionary.
What would be natural to me is being able to define multiple Codable incomingMessageHandler closures, one for each kind of closure. But I can't see an option for that anywhere - not in the direct initializers of XPCListener, nor in XPCPeerHandler.
Do you have any recommendations for this scenario? I'm pretty sure I'm doing something wrong, I'm fairly unfamiliar with the low-level XPC APIs.