Questions tagged [withcontext]
12 questions
44
votes
1 answer
kotlin coroutines, what is the difference between coroutineScope and withContext
withContext
suspend fun withContext(
context: CoroutineContext,
block: suspend CoroutineScope.() -> T
): T (source)
Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns the…

lannyf
- 9,865
- 12
- 70
- 152
9
votes
2 answers
Why does withContext await for the completion of child coroutines
The documentation of withContext states
Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns the result.
However, the actual behavior is that it awaits on all the child coroutines as well,…

Marko Topolnik
- 195,646
- 29
- 319
- 436
1
vote
2 answers
How can I wait the withContext in suspend function completes?
I am reading about Kotlin coroutine in Google 's documentation. I'm adviced to use withContext(Dispacher.IO) to a different thread to main-safety. But I have a problem , fetchData() done before response from server so fetchData() return null result.…

Trần Văn Hiền
- 31
- 6
1
vote
0 answers
'expect(...).withContext is not a function' error using Protractor
I use Protractor 7.0 and Jasemine 3.6.1, but when i use .withContext(), I get Failed: expect(...).toEqual(...).withContext is not a function error message.
I have searched the issue and the people suggest to update the Jasmine and Protractor, but it…

Ara Galstyan
- 486
- 1
- 4
- 11
1
vote
1 answer
How do I cancel a coroutine run inside a withContext?
I have a Repository defined as the following.
class StoryRepository {
private val firestore = Firebase.firestore
suspend fun fetchStories(): QuerySnapshot? {
return try {
firestore
.collection("stories")
…

Richard
- 7,037
- 2
- 23
- 76
1
vote
1 answer
is this right to use coroutines in a non coroutine context
Having a Processor class, trying to replace some of the code with coroutines. Since it is in a non coroutines context so val serviceScope = CoroutineScope(Dispatchers.IO + serviceJob) is added and used for start coroutines.
Added CoroutineScope, …

lannyf
- 9,865
- 12
- 70
- 152
0
votes
1 answer
How to wait response values in viewModelScope from two suspend functions
I have code like
viewModelScope.launch(exceptionHandler) {
withContext(Dispatchers.IO){
val name = fetchName() //suspend fun
val surname = fetchSurname() //suspend fun
}
//how to wait response from name and…

Slava
- 443
- 4
- 12
0
votes
1 answer
Cannot resolve method 'with(Context)'
I am using the latest version of android studio this my adapter program this was used in my previous projects glide version: 4.14.2 the context is showing the following error Cannot resolve method 'with(Context)'.
package…
0
votes
1 answer
Kotlin- Coroutines read file
Main function:
val users = CoroutineScope(Dispatchers.IO).launch{readFile()}.toString()
readFile:
suspend fun readFile(): String = withContext(Dispatchers.IO){
.............
return@withContext fullString
}
I'm not sure if my code is good,…
0
votes
1 answer
How to share MDC context between threads in multimodule project in kotlin - kotlinx.coroutines.slf4j
I have a multi-module project where I would like to have a context to be shared through the different modules.
In my core module (from which all of them depends on), I have created a class to manage the context using org.slf4j.MDC with two…

Lorena
- 93
- 6
0
votes
1 answer
return in coroutine with kotlin and suspend functions
IN line return@withContext cachedCategories because it can't just be return cachedCategories only. Whats @withContext ?
Code full:
@Singleton
class FoodMenuRemoteSource @Inject constructor(private val foodMenuApi: FoodMenuApi) {
private var…
0
votes
1 answer
how to cancel the coroutines jobs after timeout
Having a suspend function fetchData(). What it does is to launch a few jobs in the withContext, so that it will only return after the jobs are complete (which are: suspend fun getData(): Boolean).
And also want if it times out then return false…

lannyf
- 9,865
- 12
- 70
- 152