NWConnection connection.send completion handler examples

Hi, I can't seem to find any examples for the content processed completion handler

connection.send(content: data , contentContext: context, completion: 
NWConnection.SendCompletion.contentProcessed(({ (error) in  
  if let err = error {
          print("\(err)")
        } else {
          print("Sent successfully")
        }
 })))

I think its used to implement send side flow control, but I want to know if anyone has any good examples of it being applied that would help me understand it better.

If I'm sending a constant stream of data how can I use this completion handler to respect the back-pressure from the connection

I think its used to implement send side flow control

Correct.

If I'm sending a constant stream of data how can I use this completion handler to respect the back-pressure from the connection

It kinda depends on how you’re generating that data. The most common case is where you have to actively request new data. In that case an easy approach is:

  1. Set up a request for the first chunk of data.

  2. When they completes, call send(…) on the connection.

  3. When that completes, set up a request for the next chunk of data.

  4. Lather, rinse, repeat.

For an example of this, see the Transfer Data section of the netcat sample code. This is in C, alas, so it’s not as clean as it would be in Swift.

Also, check out the Handling Flow Copying article, which covers the same issue from a slightly different perspective.

Share and Enjoy

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

NWConnection connection.send completion handler examples
 
 
Q