Running xpc connection outside the main method

254 Views Asked by At

I was wondering if there's any limitation for the context where I initialize my xpc service.

Here's how I currently initialize my xpc service from main() which works just fine.

  listener_ = [[NSXPCListener alloc] 
     initWithMachServiceName:@"com.bla.bla"]; 
  xpcService *delegate = [xpcService new];
  listener_.delegate = delegate;
  [listener_ resume];
  [[NSRunLoop mainRunLoop] run];

However, when calling it from different method(main)/thread(main thread)... It doesn't accept remote calls, even though the listener was properly initialized.

I even tried to wrap this code to run on the main thread using the following wrapper

dispatch_sync(dispatch_get_main_queue(), ^{
  listener_ = [[NSXPCListener alloc] 
     initWithMachServiceName:@"com.bla.bla"]; 
  xpcService *delegate = [xpcService new];
  listener_.delegate = delegate;
  [listener_ resume];
}

In the example above, the [[NSRunLoop mainRunLoop] run]; is called from the main method.

So my question is what are the requirements to make the XPC work.. is it mandatory to call it from the main method ?

0

There are 0 best solutions below