【问题标题】:XPC remoteObjectWithErrorHandler wont throw an errorXPC remoteObjectWithErrorHandler 不会抛出错误
【发布时间】:2016-02-09 23:03:20
【问题描述】:

我正在尝试检测是否通过错误处理程序安装了帮助工具,但错误块不会被执行,成功块也不会执行。当助手已经安装时,它工作正常。它只是不会在有错误时捕获错误。在文档中,这两个块中的一个总是被执行

    if helperToolConnection == nil {
        let connection = NSXPCConnection(machServiceName: "**bundle identifier**", options: NSXPCConnectionOptions.Privileged)
        connection.remoteObjectInterface = NSXPCInterface(withProtocol: HelperProtocol.self)
        connection.invalidationHandler = {
            self.helperToolConnection = nil
        }
        connection.resume()
        helperToolConnection = connection
    }

    let helper = helperToolConnection!.remoteObjectProxyWithErrorHandler() { error in
        NSLog("Failed to connect: \(error)")
        withReply(nil)
    } as! HelperProtocol
    helper.connectWithEndpointReply() { endpoint -> Void in
        withReply(endpoint)
    }

【问题讨论】:

  • 那么你看到了什么效果?它会崩溃吗,如果是,在哪里以及出现什么错误?

标签: xcode swift macos xpc


【解决方案1】:

虽然我遇到了类似的问题并且尚未解决,但我想我可以帮助您解决这里错过的问题。

连接对象可以毫无怨言地创建,即使您的 XPC 服务没有安装。它也可以顺利恢复。实际的连接尝试仅在您尝试发送第一条消息时发生

所以...你的

connection.invalidationHandler = {
    self.helperToolConnection = nil
}

此时将被调用,使连接对象无效。因此,您的后续代码什么都不做 - 因为它的所有消息都发送到 nil。

要验证我的理论 - 只需在您取消 invalidationHandler 中的 helperToolConnection 之前添加一个 NSLog(),然后重试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-28
    • 2017-05-16
    • 2015-01-04
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多