Nikola Milovic
12/09/2020, 3:23 PMoverride suspend fun saveConfession(text: String): SaveConfessionResponse = suspendCoroutine {cont ->
val confessionToSave = ConfessionDataModel(text)
val call = newConfessionService.saveConfession(confessionToSave)
call.enqueue(object : Callback<SaveConfessionResponse>{
override fun onResponse(call: Call<SaveConfessionResponse>, response: Response<SaveConfessionResponse>) {
cont.resume(response.body()!!)
}
override fun onFailure(call: Call<SaveConfessionResponse>, t: Throwable) {
cont.resumeWithException(t)
}
})
}
And I am trying to call it from my useCase object
suspend fun execute(text: String): Result = suspendCoroutine{ cont->
try {
val response = confessionRepository.saveConfession(text)
when (response.status) {
200 -> Result.Success(response.id, 200)
400 -> Result.Error(null)
500 -> Result.Error(null)
else -> Result.Error(null)
}
} catch (e: Exception) {
return Result.Error(e)
}
}
/
But i cannot call saveConfession outside of coroutine body, can I somehow get the scope that the execute function was called from?Marc Knaup
12/09/2020, 3:24 PMsuspendCoroutine
? That shouldn’t be there.Nikola Milovic
12/09/2020, 3:25 PMNikola Milovic
12/09/2020, 3:26 PMMarc Knaup
12/09/2020, 3:26 PMexecute()
🤔Marc Knaup
12/09/2020, 3:26 PMcont
to continue the coroutine, the coroutine would hang forever.Marc Knaup
12/09/2020, 3:27 PMsaveConfession
it makes sense because you use callbacks.Nikola Milovic
12/09/2020, 3:28 PMsaveConfession
then and wait for it to give me a result before returning from the execute
?Marc Knaup
12/09/2020, 3:28 PMsuspendCoroutine
and make it a normal suspend fun
.Nikola Milovic
12/09/2020, 3:33 PMmyanmarking
12/09/2020, 4:08 PM