question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support runBlocking with arbitrary context

See original GitHub issue

The current behavior of runBlocking with a specified context is somewhat low-level and hardly used in practice. In particular, unless the dispatcher that is passed to runBlocking implements EventLoop (usually - not), then runBlocking creates its own private event loop and uses it as dispatcher, while ignoring the dispatcher that was passed to it in the context.

This proposal is to change this behavior so that runBlocking(UI) { ... } would result in blocking the current thread and running the code in the context of UI dispatcher, consistently with how other coroutine builders (launch, async, etc) operate. It looks that this is going to be less-surprising behavior.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
elizarovcommented, Jan 29, 2018

Let me clarify the consistency here. For all other coroutine builders like launch, using them with a context:

launch(ctx) { ... }

is equivalent to using them without context, but relying on withContext to change it:

launch { withContext(ctx) { ... } }

This change essentially brings the same behavior to runBlocking function.

2reactions
gildorcommented, Jan 29, 2018

I think runBlocking + optional context is better, I don’t see why we should add one more function, Kotlin default arguments allow to avoid additional method without any drawbacks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does runBlocking(Dispatchers.IO) block the main thread even ...
When CoroutineDispatcher is explicitly specified in the context, then the new coroutine runs in the context of the specified dispatcher ...
Read more >
Guide to reactive streams with coroutines
Coroutine context to rule them all. A coroutine is always working in some context. For example, let us start a coroutine in the...
Read more >
runBlocking in Kotlin Coroutines with Example - GeeksforGeeks
runBlocking is a coroutine function. By not providing any context, it will get run on the main thread.Runs a new coroutine and blocks...
Read more >
Kotlin Coroutines 1.5: GlobalScope Marked as Delicate ...
If the network is slow, it keeps waiting in the background, consuming resources. ... fun main() = runBlocking<Unit> {
Read more >
Kotlin Coroutines Concurrency - Kotlin Expertise Blog
Coroutines provide sophisticated means which help us structure code to ... The work on all tasks may be interleaved in some arbitrary order, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found