为什么 querySkuDetails 需要在 IO 上下文中 运行?

Why does the querySkuDetails need to run in IO context?

根据 https://developer.android.com/google/play/billing/integrate billingClient.querySkuDetails 被调用 withContext(Dispatchers.IO)

fun querySkuDetails() {
    val skuList = ArrayList<String>()
    skuList.add("premium_upgrade")
    skuList.add("gas")
    val params = SkuDetailsParams.newBuilder()
    params.setSkusList(skuList).setType(SkuType.INAPP)
    val skuDetailsResult = withContext(Dispatchers.IO) {
        billingClient.querySkuDetails(params.build())
    }
    // Process the result.
}

我很好奇它有什么好处,因为querySkuDetails已经是一个暂停功能了。那我在这里有什么收获。

我可以用

编写相同的代码
val skuDetailsResult = coroutineScope {
        billingClient.querySkuDetails(params.build())
}

没有更多的上下文,我不知道如何下载计费客户端的源代码。

被调用的基础方法是 querySkuDetailsAsync,它接受回调并异步执行网络请求。

你是对的,那里不需要 withContext(Dispatchers.IO),它实际上引入了不必要的开销。

来自

It seems to be a common misconception, that just because IO is being performed by a suspend function, you must call it in Dispatchers.IO, which is unnecessary (and can be expensive).

suspending functions by convention don't block the calling thread and internally blocks in Dispatchers.IO if need be.