问题描述
我的父应用程序使用助手XPC完成某些任务T1.XPC服务是使用NSXPCConnection实施的.任务完成后,我就不需要此XPC了.因此,父进程在NSXPCConnection对象上调用了invalidate.但是,即使连接无效,XPC仍然有效.我可以看到它在活动监视器"中列出.
My parent application uses helper XPC to achieve some task T1. XPC service is implemented using NSXPCConnection.Once the task is complete I don’t need this XPC to be alive. Hence, the parent process calls invalidate on NSXPCConnection object.But, the XPC is still alive even if the connection is invalid. I can see it getting listed in Activity Monitor.
当父应用程序仍在运行且不需要XPC存活时,如何杀死此XPC?
How do I kill this XPC when parent app is still running and it does not require XPC to be alive?
推荐答案
请注意,当父应用程序退出时,您的XPC进程也会退出.至少我的确实如此.您的问题是:在此之前如何杀死它?
Just to be clear, your XPC process will quit when the parent app quits. At least, mine does. Your question is: How to kill it before then?
这很简单:只需在XPC进程中调用完成退出事件处理程序(将输出发送回父应用程序)后立即在XPC进程中调用 exit(0)
即可.上个月,我需要在一个项目中进行此操作,因为XPC流程正在使用Apple私有框架,退出它是我想阻止某些不希望的和无法解释的唯一方法(因为没有关于私有框架的文档)的副作用.
That is simple: Just call exit(0)
in your XPC process immediately after it invokes the completion handler which sends the output back to the parent app. I needed to do this in a project last month, because the XPC process was using an Apple private framework, and exitting it was the only way I could figure to stop a certain undesired and inexplicable (since I have no documentation on the private framework) side effect.
但是,退出这个XPC进程后,我发现奇怪的是,它继续运行了几毫秒,并且如果我的父应用程序与此同时打开了与此同一个XPC服务的另一个连接,那么注定失败的XPC进程将接受新的连接,然后杀死自己的父应用程序.实际上,我已经多次看到这种情况.
Upon exitting this XPC process, however, I found that, oddly, it continued to run for some milliseconds, and if my parent app opened another connection to this same XPC service in the meantime, the doomed XPC process will accept the new connection just prior to killing itself, leaving the parent app hanging. I've actually seen this happen several times.
因此,一个后续问题是:您是否只是因为想变得友善就杀了它?根据以下引文,我认为答案是否.从Apple的守护程序和服务编程指南>创建XPC服务
So, a follow-up question is: Should you kill it just because you want to be nice? I think the answer is no, according to the following quote from Apple's Daemons and Services Programming Guide > Creating XPC Services
总结:完成后只是使您的连接无效,并让启动时担心中断.另外,不要屏住呼吸.我过去两个小时一直在运行我的应用程序,并且最近两个小时关闭的XPC进程仍在运行.
To summarize: Just invalidate your connections when done, and let launchd worry about killing. Also, don't hold your breath. I've had my app running for the past two hours, and the XPC process, last connection closed two hours ago, is still running.
这篇关于当父应用仍在运行时杀死XPC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!