Can an XPC service send a response in piecemeal?

Just trying to understand the documentation.

Obviously, we can send a request to the service to return all the data at once. Can the data arrive in pieces, involving either multiple async callbacks or a Combine Publisher?

Answered by DTS Engineer in 865926022

XPC uses a request/response architecture. When an XPC API calls you to handle a request, it typically [1] give you a reply handler and you are required to call that once and only once.

However, that does not prevent you from streaming data to the client. There are lots of ways you can do that. There are two basic approaches for you to explore:

  • XPC-based options — See XPC Resources for a link to my discussion on bidirectional communication.
  • Non-XPC-based options — For example, you could create a socket pair and pass one end back to the client via XPC’s descriptor passing support.

IMPORTANT If your server is in a privileged position, it must protect itself from rogue clients. Specifically, if the client stops reading data, the server must not run itself out of memory. This is particularly important on iOS and its child platforms, where the client might get suspended. If you’re using NSXPConnection, the scheduleSendBarrierBlock(_:) call is a good way to do this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Unless it’s a one-way message, in which you get no reply handler.

XPC uses a request/response architecture. When an XPC API calls you to handle a request, it typically [1] give you a reply handler and you are required to call that once and only once.

However, that does not prevent you from streaming data to the client. There are lots of ways you can do that. There are two basic approaches for you to explore:

  • XPC-based options — See XPC Resources for a link to my discussion on bidirectional communication.
  • Non-XPC-based options — For example, you could create a socket pair and pass one end back to the client via XPC’s descriptor passing support.

IMPORTANT If your server is in a privileged position, it must protect itself from rogue clients. Specifically, if the client stops reading data, the server must not run itself out of memory. This is particularly important on iOS and its child platforms, where the client might get suspended. If you’re using NSXPConnection, the scheduleSendBarrierBlock(_:) call is a good way to do this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Unless it’s a one-way message, in which you get no reply handler.

Can an XPC service send a response in piecemeal?
 
 
Q