> For the complete documentation index, see [llms.txt](https://android-course.cornellappdev.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://android-course.cornellappdev.com/chapters/5.5-concurrency/8.4-coroutines-with-networking-calls.md).

# 5.5.3 Coroutines with Networking Calls

## Coroutines with Networking Calls

All Kotlin coroutines run on dispatchers, and there are three types: `Main`, `IO`, and `Default`. It is perfectly okay to run suspending function on the main thread as long as the functions do not take a long time. Generally, networking and file IOs calls are ran on the IO thread to prevent consuming all the resources on the main thread.&#x20;

Here are the general guidelines for which dispatcher to use:&#x20;

| Dispatcher Type | Responsibilities                                                                                 |
| --------------- | ------------------------------------------------------------------------------------------------ |
| Main            | <ul><li>updating states</li><li>callings <code>suspend</code> and UI related functions</li></ul> |
| IO              | <ul><li>making networking requests</li><li>reading and writing to a file or database</li></ul>   |
| Default         | <ul><li>CPU intensive operations like parsing JSON or sorting a list</li></ul>                   |

Here's an example of how to make a networking call with coroutines:&#x20;

```kotlin
// Dispatchers.Main
suspend fun functionA() {
    val result = get("developer.android.com")
    show(result)
}

suspend fun get(url: String) = 
    // Dispatchers.IO
    withContext(Dispatchers.IO) { actualNetworkingCall() }

```

As we see in the example, the main function `functionA()` runs on the main thread, but the actual networking call occurs on the IO thread. This allows the outer function to complete other tasks as it waits for the networking call to complete itself.&#x20;

Example code blocks adapted from <https://kotlinlang.org/>.&#x20;


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://android-course.cornellappdev.com/chapters/5.5-concurrency/8.4-coroutines-with-networking-calls.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
