ClientStreaming in V2

The APIs in v2 are designed to enforce structured concurrency which is why when the closure returns in client-streaming calls the client half closes and waits for the server response. This means that from inspection it's easy to see when the RPC has completed (and when any related resources will have been freed).

let response = client.clientStreaming { writer in 
  // ...
}
// At this point the RPC has finished.

If you restructure your code to use structured concurrency then you have a few options:

  1. Store the writer in some internal state and block the closure from returning until you've finished writing
  2. Use an AsyncStream to bridge between unstructured and structure code. You can write into the AsyncStream.Continuation and consume the stream within the body of the client streaming RPC.
let response = client.clientStreaming { writer in 
  for await message in myStream {
    try await writer.write(message)
  }
}