【发布时间】:2020-02-11 15:50:26
【问题描述】:
我正在使用 Kotlin 和 RxJava 开发一个 android 应用程序。
我使用 android 最新 API 开发了应用内结算。
但是这个库只支持异步回调。
所以我写了这样的代码:
private fun consumePlayStore(consumeParams: ConsumeParams, secKey: String, purchase: Purchase): Single<Pair<String, Purchase>> {
return Single.create<Pair<String, Purchase>> { emitter ->
Log.d("TEST", "[Billing] consumePlayStore") // IO Thread
// Google InApp Library call
playStoreBillingClient?.consumeAsync(consumeParams) { result, _ ->
// In here, thread is Main!!! Why!!!
Log.d("TEST", "[Billing] consumePlayStore - response") // Main Thread
}
}
}
fun consume() {
verifyCompletable.andThen(consumePlayStore())
.flatMap(otherJob)
.subscribeOn(ioScheduler)
.observeOn(uiScheduler)
.subscribe()
}
不知道为什么在回调中改线程?
谁能告诉我为什么?
我该如何解决这个问题???
或者更好的设计???
【问题讨论】: