0

I am trying to make a coroutine from a method that I have. to make things simple, let's say I have a class A that I try to connect() and it is connected only after class B that is inside A is connected.

So, I have this code for example, which offcourse doesn't work but it's just to show my use case-

class A {
    fun connect() {
        classB.connect()
        val isConnected = classB.isConnected
    }
}

class B {
    val isConnected: Boolean = false
    fun connect() {
        someObject.connect( SomeListenerInterface {
            override fun onSuccess() {
                isConnected = true
            }
        })
    }
}

I want to make the classB.connect() as a coroutine, and make it suspended, so only when it is done, the line of val isConnected = classB.isConnected would execute and the value would be set properly.

If I would use java and callbacks, I would just pass a callback to the classB.connect() method, and set the class A.isConnected value inside this callback.

is it possible with kotlin coroutines? Thanks

3
  • The first thing you need here is convert you callback based someObject.connect function into a suspend function. You can do that using suspendCancellableCoroutine builder. Now that someObject.connect is suspend, you need to mark B.connect as suspend. The last bit required is a CoroutineScope which is a bridge between non-coroutine and coroutine based world. Since you haven't provided the exact code and mentioned what it is doing, it's not possible to suggest the coroutine code but what I wrote earlier is the gist of what you need to do. Commented Nov 27, 2021 at 17:43
  • @ArpitShukla thanks for the suggestion. I think I will just use something like val ack = CompletableDeferred<Boolean>() inside classB and I will write ack.complete(true) inside classB.connect() and in classA.connect() I will just do classB.await() after the classB.connect(). what do you think? Commented Nov 28, 2021 at 10:57
  • @StackerSapper this is unnecessary, you should instead follow what Arpit said, which is detailed in Kirill's answer Commented Dec 6, 2021 at 23:32

1 Answer 1

3

Convert callback to suspend function using suspendCoroutine. The function below returns true when the connection succeeds:

class B {
    suspend fun connect(): Boolean = suspendCoroutine { continuation ->
        someObject.connect(object : SomeListenerInterface {
            override fun onSuccess() {
                continuation.resume(true)
            }
        })
    }
}

connect function in class A should be suspend respectively:

class A {
    suspend fun connect() {
        val isConnected = classB.connect()
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you, though does the solution with CompletableDeferred<Boolean>() isn't good also? because I have many callbacks that are relatd to each other
I relied on the Roman Elizarov, the Kotlin founder, opinion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.