0

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 ?

Zohar81
  • 4,554
  • 5
  • 29
  • 82
  • Are `listener_ ` and `delegate` deallocated when they go out of scope? – Willeke Jan 10 '22 at 22:39
  • This code is called from C++ method in class and the listener_ is a member of this class... the class is singleton so I the lifetime of `lisener_` will remain... as for the `delegate` I guess it lives as long as `listener_` lives since it has reference. is it a good practice ? do i need to use bridge here ? – Zohar81 Jan 11 '22 at 10:18
  • `delegate` is a weak reference. – Willeke Jan 11 '22 at 10:26
  • you're right. so If i declare delegate as a class member, it should last beyond the scope of this function. right? – Zohar81 Jan 11 '22 at 12:19
  • Yes, or make it a singleton. – Willeke Jan 11 '22 at 13:00
  • Have you tried `[NSRunLoop.currentRunLoop run]` instead? – battlmonstr Jan 12 '22 at 23:25

0 Answers0