gpt4 book ai didi

objective-c - NSXPCConnection 通过代理传递一个对象

转载 作者:太空狗 更新时间:2023-10-30 03:41:57 25 4
gpt4 key购买 nike

Daemons and Services Programming Guides告诉我们可以通过一个开放的 XPC 连接返回一个代理对象,甚至作为回复 block 参数。

通过代理传递对象

Most of the time, it makes sense to copy objects and send them to the other side of a connection. However, this is not always desirable. In particular:

If you need to share a single instance of the data between the client application and the helper, you must pass the objects by proxy. If an object needs to call methods on other objects within your application that you cannot or do not wish to pass across the connection (such as user interface objects), then you must pass an object by proxy—either the caller, the callee (where possible), or a relay object that you construct specifically for that purpose. The downside to passing objects by proxy is that performance is significantly reduced (because every access to the object requires interprocess communication). For this reason, you should only pass objects by proxy if it is not possible to pass them by copying.

You can configure additional proxy objects similarly to the way you configured the remoteObjectInterface property of the initial connection. First, identify which parameter to a method should be passed by proxy, then specify an NSXPCInterface object that defines the interface for that object.

第一个问题来了:proxy传递的对象应该怎么定义?作为符合 NSXPCProxyCreating 协议(protocol)的对象?那么 remoteObjectProxy 和 remoteObjectProxyWithErrorHandler: 方法应该实现吗?

下面是一个例子,我一点都不清楚。特别是我不明白我应该在哪里调用 NSXPCInterface 方法 (setInterface:forSelector:argumentIndex:ofReply:) 来将参数作为代理列入白名单:在 XPC 服务代码中还是在主机中?

The first parameter to a method is parameter 0, followed by parameter 1, and so on.

In this case, the value NO is passed for the ofReply parameter because this code is modifying the whitelist for one of the parameters of the method itself. If you are whitelisting a class for a parameter of the method’s reply block, pass YES instead.

所以问题是:任何人都可以为我提供一个清晰的教程,说明如何在 XPC 方法调用的 block 回复中将对象作为代理返回吗?

最佳答案

我现在可以回答我自己的问题了:要在 XPC 方法调用的 block 回复中返回一个对象作为代理,应该同时调用 setInterface:forSelector:argumentIndex:ofReply: 方法:

  • 在 XPC 服务的端点中,声明了 exportedInterface
  • 在主机中,声明了remoteObjectInterface

即通用代码:

// common (service/host) protocol definition
@protocol Service
@end

@protocol ServiceFactory
-(void)connectToNewService: (void (^)(id<Service>)reply;
@end

在 XPC 服务中:

// Implement the one method in the NSXPCListenerDelegate protocol.
-(BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection*)newConnection {

NSXPCInterface *serviceFactoryInterface =[NSXPCInterface interfaceWithProtocol:@protocol(ServiceFactory)];
NSXPCInterface *serviceInterface =[NSXPCInterface interfaceWithProtocol:@protocol(Service)];

// connection has to be returned as proxy, not as a copy
[serviceFactoryInterface setInterface: serviceInterface
forSelector: @selector(connectToNewService:)
argumentIndex: 0
ofReply: YES];

newConnection.exportedInterface = serviceFactoryInterface;
newConnection.exportedObject = self;

[newConnection resume];
return YES;

}

在主机代码中:

// in the host

- (void)openNewService
{
NSXPCConnection *xpcConnection = [[NSXPCConnection alloc] initWithServiceName:@"eu.mycompany.servicefactory"];
NSXPCInterface *serviceFactoryInterface =[NSXPCInterface interfaceWithProtocol:@protocol(ServiceFactory)];
NSXPCInterface *serviceInterface =[NSXPCInterface interfaceWithProtocol:@protocol(Service)];

// connection has to be returned as proxy, not as a copy
[serviceFactoryInterface setInterface: serviceInterface
forSelector: @selector(connectToNewService:)
argumentIndex: 0
ofReply: YES];

xpcConnection.remoteObjectInterface = serviceFactoryInterface;
[xpcConnection resume];

[[xpcConnection remoteObjectProxy] connectToNewService:^(id<Service> newService) {

// here a newService is returned as NSXPCDistantObject <Service>*
[xpcConnection invalidate];

}];

}

关于objective-c - NSXPCConnection 通过代理传递一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14661375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com