Coroutine Scopes
Every coroutine builder launches its coroutines inside a coroutine scope. A coroutine’s scope has control over how the coroutine’s code executes. This includes setting up the coroutine, canceling the coroutine, and choosing which thread will be used to run the code. At the moment, you are using GlobalScope as the coroutine scope to perform your network requests.
GlobalScope provides an easy way to launch a coroutine, so it is useful as a first example here. But we do not actually recommend it for most applications, since it can introduce problems into your code. In this case, it is the cause of the problem you are seeing: GlobalScope does not keep your process alive long enough to wait for the response to return and ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access