0

Converting my own http calls to KTor (& therefore coroutines) is a bit hollow, or at least not terribly satisfying, without also being able to convert Play Store API calls and their callbacks to use suspend functions.

This general answer, with specific example of a different API, is fabulous and I've managed to use it to produce something for "startBillingClient", as follows

private suspend fun BillingClient.startConnection(value: Int) = 
    suspendCoroutine<Unit> { cont ->
        val callback = object: BillingClientStateListener {
            override fun onBillingSetupFinished(billingResult: 
                                              BillingResult) {
                isServiceConnected = true
            }

            override fun onBillingServiceDisconnected() {
                isServiceConnected = false
                startConnection(this)
            }
        }
    startConnection(callback)
}

but now I'm struggling with producing something similar for querySkuDetailsAsync, which seems to be due to needing to pass parameters in. Should be easy, but...

Markers
  • 328
  • 1
  • 13
  • You didn't implement it right. The coroutine will never resume. I'm not sure if a `suspend fun` is even the right tool for what you're doing. Sounds like a long-running background task and the listener is responding to a sequence of events instead of a single event that represents the completion of the action. – Marko Topolnik Mar 15 '21 at 11:09
  • HI @Marko, I think this *is* a response to a single event, starting the Play Store billing client. Aren't all the API calls for the Play Store single event/single response calls. Back to here, though, what's needed, cont.resume? – Markers Mar 15 '21 at 20:17
  • 1
    Yes, `cont.resume` with nothing else inside. It's not a good practice to have business logic inside the `suspendCoroutine` block. Its only purpose is adapting from an async API to a `suspend fun`. – Marko Topolnik Mar 15 '21 at 20:43

0 Answers0