I've successfully followed the Daemons and Service Guide - Creating XPC Services tutorial, along with the SandboxedFetch
sample code and have a working Client / Service setup that is using the new NSXPCConnection
class.
What still isn't entirely clear to me is how to properly self-terminate the service application once it has finished its job. I recognize that in many cases, the service is expected to remain alive, but in the use-case I have in mind, the service will be used to only to do some processing that I'd otherwise not perform in the main application. Once that processing is finished, there's no reason for the service application to remain. If the client needs another service at a later date, it can just re-create a new one.
Since the service is a lightweight, non-nibbed, NSApplication
, I was trying to self-terminate it by calling invalidate
from inside applicationWillTerminate
, but that triggers an EXC_BAD_ACCESS exception almost every time. Calling invalidate
on the service's [NSXPCListener serviceListener]
generates slightly less reliable crashes, but it still crashes.
Calling invalidate
from within the client application on its NSXPCConnection
also generates an EXC_BAD_ACCESS exception almost every time.
So I'm curious what the correct sequence of steps is to cleanly shutdown the XPC service and then quit the service application. Ideally, the service would self-terminate after it has made its last XPC call to the client.
Attached is a small screenshot of what one of the exception's stacktrace looks like. (Yes, that's a webview that's loading in the service. Once the webview has finished loading, I want the service to self-terminate)
launchd
automatically when needed. – Clearing