How to Exponential Backoff retry on kotlin coroutines

I would suggest to write a helper higher-order function for your retry logic. You can use the following implementation for a start: suspend fun <T> retryIO( times: Int = Int.MAX_VALUE, initialDelay: Long = 100, // 0.1 second maxDelay: Long = 1000, // 1 second factor: Double = 2.0, block: suspend () -> T): T { … Read more

Using rememberCoroutineScope() vs LaunchedEffect

Leaving my understanding here: Question 1: LaunchedEffect should be used when you want that some action must be taken when your composable is first launched/relaunched (or when the key parameter has changed). For example, when you want to request some data from your ViewModel or run some sort of animation… rememberCoroutineScope on the other hand, … Read more

Unresolved reference: launch

Launch isn’t used anymore directly. The Kotlin documentation suggests using: fun main() { GlobalScope.launch { // launch a new coroutine in background and continue delay(1000L) println(“World!”) } println(“Hello,”) // main thread continues here immediately runBlocking { // but this expression blocks the main thread delay(2000L) // … while we delay for 2 seconds to keep … Read more

Unit testing coroutines runBlockingTest: This job has not completed yet

As can be seen in this post: This exception usually means that some coroutines from your tests were scheduled outside the test scope (more specifically the test dispatcher). Instead of performing this: private val networkContext: CoroutineContext = TestCoroutineDispatcher() private val sut = Foo( networkContext, someInteractor ) fun `some test`() = runBlockingTest() { // given … … Read more

Suspend function ‘callGetApi’ should be called only from a coroutine or another suspend function

Suspend function should be called only from a coroutine. That means to call a suspend function you need to use a coroutine builder, e.g. launch, async or runBlocking(recommended to use only in unit tests). For example: class Activity : AppCompatActivity(), CoroutineScope { private var job: Job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main … Read more

Call Kotlin suspend function in Java class

First, add org.jetbrains.kotlinx:kotlinx-coroutines-jdk8 module to your dependencies. In your Kotlin file define the following async function that corresponds to Java style of writing async APIs: fun doSomethingAsync(): CompletableFuture<List<MyClass>> = GlobalScope.future { doSomething() } Now use doSomethingAsync from Java in the same way as you are using other asynchronous APIs in the Java world.

Implementing coroutines in Java

I would take a look at this: http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html, its pretty interesting and should provide a good place to start. But of course we are using Java so we can do better (or maybe worse because there are no macros :)) From my understanding with coroutines you usually have a producer and a consumer coroutine (or … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)