Comments (4)
Yes, runBlocking
blocks, as the name suggests. Alternatively, use launch
to start a coroutine without waiting for it. In any way, you can't just call suspend
functions from non-suspend
functions because they are compiled completely different. That's just how coroutines work.
from kotlinx.coroutines.
While this issue probably for Youtrack, I'll answer here:
You can always do runBlocking<Unit> {}
in your non-suspend
functions to cross the boundry to the coroutines world.
from kotlinx.coroutines.
@cypressious the workaround you propose is not the same, in fact, if I am not wrong, runBlocking<Unit> {}
blocks the calling thread.
In addition, it uses a different context than of the one of the caller and it should be used only as top-level coroutine based on the documentation:
runBlocking { ... }
works as an adaptor that is used here to start the top-level main coroutine. The regular code outside of runBlocking blocks, until the coroutine inside runBlocking is active.
from kotlinx.coroutines.
It is not just the different compilation. Suspending and non-suspending functions are conceptually different. suspend
function can suspend the execution, while regular functions cannot do it conceptually. Think about it as a part of a function's type. For example, if your function is declared to return a String
it cannot just go ahead and return an integer 32 instead. Similarly, if your function is declared with a guarantee that it never suspends (a regular function), it cannot just decide go ahead and decide suspend. That is so by design.
from kotlinx.coroutines.
Related Issues (20)
- `withContext` may execute code in the wrong context if the `coroutineContext` misleads it HOT 3
- SharedFlow doesn't have same parameters as in constructor function HOT 2
- Consider deprecation cycle for `CoroutineDispatcher.invoke` HOT 6
- How to prevent a SharedFlow collect values when the activity resumes? HOT 1
- Consider discouraging `CoroutineStart.LAZY`
- Some problem about `addLast` in LockFreeTaskQueue HOT 1
- Maybe it's reasonable to recommend wrapping a `callbackFlow` initialization into `try`-`finally`? HOT 4
- Dispatcher failures may leave coroutines uncompleted HOT 1
- `DelayWithTimeoutDiagnostics` missing from R8 rules? HOT 3
- Introduce Flow.all/any/none operators HOT 3
- [WASM] JsException: Exception was thrown while running JavaScript code kotlinx.coroutines.error_$external_fun HOT 6
- Suppressed exceptions are lost during stacktrace recovery HOT 1
- `runBlocking` executes other tasks instead of its own HOT 1
- "Kotlin Compiler Error: NoClassDefFoundError for kotlin/reflect/full/KClasses during Gradle build" HOT 1
- Add argument to sample to sample at the start of a flow instead of the end? HOT 5
- Mapping a list asynchronously HOT 2
- rx Flowable that use Unconfined dispatcher result in a deadlock when using a runblocking() that execute a suspend function with (Semaphore, mutex or ReentrantLock) HOT 1
- Some way of subscribe to shared flow without slowing-down others HOT 7
- Wrong function's return type called inside lambda when using launch HOT 11
- Flow collect may cause the Gamepad testing failed when I use webview to loading the Gamepad testing url after used collect function HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlinx.coroutines.