diff --git a/examples/android/src/main/kotlin/com/connectrpc/examples/android/ElizaChatActivity.kt b/examples/android/src/main/kotlin/com/connectrpc/examples/android/ElizaChatActivity.kt index e4f82802..0acd934c 100644 --- a/examples/android/src/main/kotlin/com/connectrpc/examples/android/ElizaChatActivity.kt +++ b/examples/android/src/main/kotlin/com/connectrpc/examples/android/ElizaChatActivity.kt @@ -86,6 +86,9 @@ class ElizaChatActivity : AppCompatActivity() { host = host, serializationStrategy = GoogleJavaLiteProtobufStrategy(), networkProtocol = selectedNetworkProtocolOption, + // RPC operations that involve network I/O will + // use this coroutine context. + ioCoroutineContext = Dispatchers.IO, ), ) // Create the Eliza service client. @@ -113,7 +116,7 @@ class ElizaChatActivity : AppCompatActivity() { adapter.add(MessageData(sentence, false)) editTextView.setText("") // Ensure IO context for unary requests. - lifecycleScope.launch(Dispatchers.IO) { + lifecycleScope.launch { // Make a unary request to Eliza. val response = elizaServiceClient.say(SayRequest.newBuilder().setSentence(sentence).build()) response.success { success -> @@ -133,7 +136,7 @@ class ElizaChatActivity : AppCompatActivity() { private fun setupStreamingChat(elizaServiceClient: ElizaServiceClient) { // On stream result, this callback can be called multiple times. - lifecycleScope.launch(Dispatchers.IO) { + lifecycleScope.launch { // Initialize a bidi stream with Eliza. val stream = elizaServiceClient.converse() try { @@ -156,15 +159,13 @@ class ElizaChatActivity : AppCompatActivity() { } catch (e: ConnectException) { adapter.add(MessageData("Session failed with code ${e.code}", true)) } - lifecycleScope.launch(Dispatchers.Main) { - buttonView.setOnClickListener { - val sentence = editTextView.text.toString() - adapter.add(MessageData(sentence, false)) - editTextView.setText("") - // Send will be streaming a message to Eliza. - lifecycleScope.launch(Dispatchers.IO) { - stream.send(ConverseRequest.newBuilder().setSentence(sentence).build()) - } + buttonView.setOnClickListener { + val sentence = editTextView.text.toString() + adapter.add(MessageData(sentence, false)) + editTextView.setText("") + // Send will be streaming a message to Eliza. + lifecycleScope.launch { + stream.send(ConverseRequest.newBuilder().setSentence(sentence).build()) } } } diff --git a/examples/kotlin-google-java/src/main/kotlin/com/connectrpc/examples/kotlin/Main.kt b/examples/kotlin-google-java/src/main/kotlin/com/connectrpc/examples/kotlin/Main.kt index 3d3181da..e2508537 100644 --- a/examples/kotlin-google-java/src/main/kotlin/com/connectrpc/examples/kotlin/Main.kt +++ b/examples/kotlin-google-java/src/main/kotlin/com/connectrpc/examples/kotlin/Main.kt @@ -22,7 +22,6 @@ import com.connectrpc.impl.ProtocolClient import com.connectrpc.okhttp.ConnectOkHttpClient import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.withContext import okhttp3.OkHttpClient import java.time.Duration @@ -44,6 +43,9 @@ class Main { ProtocolClientConfig( host = host, serializationStrategy = GoogleJavaProtobufStrategy(), + // RPC operations that involve network I/O will + // use this coroutine context. + ioCoroutineContext = Dispatchers.IO, ), ) val elizaServiceClient = ElizaServiceClient(client) @@ -57,13 +59,11 @@ class Main { private suspend fun connectStreaming(elizaServiceClient: ElizaServiceClient) { val stream = elizaServiceClient.converse() - withContext(Dispatchers.IO) { - // Add the message the user is sending to the views. - stream.send(converseRequest { sentence = "hello" }) - stream.sendClose() - for (response in stream.responseChannel()) { - println(response.sentence) - } + // Add the message the user is sending to the views. + stream.send(converseRequest { sentence = "hello" }) + stream.sendClose() + for (response in stream.responseChannel()) { + println(response.sentence) } } } diff --git a/examples/kotlin-google-javalite/src/main/kotlin/com/connectrpc/examples/kotlin/Main.kt b/examples/kotlin-google-javalite/src/main/kotlin/com/connectrpc/examples/kotlin/Main.kt index 8205d4ad..7e6179ee 100644 --- a/examples/kotlin-google-javalite/src/main/kotlin/com/connectrpc/examples/kotlin/Main.kt +++ b/examples/kotlin-google-javalite/src/main/kotlin/com/connectrpc/examples/kotlin/Main.kt @@ -22,7 +22,6 @@ import com.connectrpc.impl.ProtocolClient import com.connectrpc.okhttp.ConnectOkHttpClient import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.withContext import okhttp3.OkHttpClient import java.time.Duration @@ -44,6 +43,9 @@ class Main { ProtocolClientConfig( host = host, serializationStrategy = GoogleJavaLiteProtobufStrategy(), + // RPC operations that involve network I/O will + // use this coroutine context. + ioCoroutineContext = Dispatchers.IO, ), ) val elizaServiceClient = ElizaServiceClient(client) @@ -57,13 +59,11 @@ class Main { private suspend fun connectStreaming(elizaServiceClient: ElizaServiceClient) { val stream = elizaServiceClient.converse() - withContext(Dispatchers.IO) { - // Add the message the user is sending to the views. - stream.send(converseRequest { sentence = "hello" }) - stream.sendClose() - for (response in stream.responseChannel()) { - println(response.sentence) - } + // Add the message the user is sending to the views. + stream.send(converseRequest { sentence = "hello" }) + stream.sendClose() + for (response in stream.responseChannel()) { + println(response.sentence) } } }