I have a little problem. I created a retriable fun...
# announcements
r
I have a little problem. I created a retriable function, but when I try to call it I always get an error:
Copy code
Suspension functions can be called only within coroutine body
Sem título
DeepinScreenshot_select-area_20200406215558.png
z
What is the signature of the
forEach
method on
records
?
Also that is a very large inline function, you may want to consider factoring most of the contents out so you’re not copying that giant body around to all your call sites.
r
I know, I'm first writing the full function and then splitting it up. The forEach signature is the default for the kotlin.
z
Kotlin stdlib defines multiple
forEach
functions, with different signatures.
r
this one
Copy code
public inline fun <T> Iterable<T>.forEach(action: (T) -> Unit): Unit {
    for (element in this) action(element)
}
z
But that’s not the issue, i don’t think. What’s the signature of
producerEvent
?
r
Sem título
z
It’s complaining because your
onErrorCallback
and
onSuccessCallback
parameter types are regular functions, not suspend functions, so you can’t call
producerEvent
from them, which is a suspend function.
r
so.. i need to do something like onErrorCallback:
suspend
() -> Unit
z
That should fix it, yep.
You could also make
retriable
inline, and not suspending, since it’s just a loop+try/catch. Inline functions’ function arguments can be suspending if the inline function is called from a suspend function.
r
need to be
suspend
because of the
delay
z
Ah right. Would still recommend making this inline though, since one of the main purposes of inline functions is to reduce lambda overhead for simple wrappers like this.
Although it’s a bit odd to be passing callbacks to a suspend function like this. It would be more idiomatic for
retriable
to just throw the exception.
👍 1
r
Thx for all help @Zach Klippenstein (he/him) [MOD]