gpt4 book ai didi

android - 如何使用使用回调的第三方库实现 CoroutineWorker?

转载 作者:行者123 更新时间:2023-11-29 02:19:43 29 4
gpt4 key购买 nike

我正在尝试实现 CoroutineWorker 以在 Android 应用程序中执行一些后台工作。我正在使用的第三方库使用回调,如 onConnected、onChanged 等。如何在 CoroutineWorker 中使用此库?

这是我目前的情况

override suspend fun doWork(): Result {
return try {
val appContext = applicationContext
var mReporter: StepCountReporter?

val mStepCountObserver = object : StepCountReporter.StepCountObserver {
override fun onChanged(count: Int) {
Log.d(APP_TAG, "Step reported : $count")
// This is where the work is completed
}
}

val mConnectionListener = object : HealthDataStore.ConnectionListener {

override fun onConnected() {
Log.d(APP_TAG, "Health data service is connected.")
mReporter = StepCountReporter(mStore!!)
if (isPermissionAcquired) {
mReporter!!.start(mStepCountObserver)
} else {
Log.e(APP_TAG, "permissions not acquired")
}
}

override fun onConnectionFailed(error: HealthConnectionErrorResult) {
Log.d(APP_TAG, "Health data service is not available.")
}

override fun onDisconnected() {
Log.d(APP_TAG, "Health data service is disconnected.")
}
}
mStore = HealthDataStore(appContext, mConnectionListener)
mStore!!.connectService()
// wait for mStepCountObserver.onChanged to be called
} catch (error: Throwable) {
Result.failure()
}
}

我正在尝试完成 mStepCountObserver.onChanged 中的协程,但看起来我应该在函数末尾调用 Result.success

最佳答案

您可以使用suspendCoroutine 函数,它有助于将协程与回调连接起来。

“suspendCoroutine”暂停调用它的协程,并且仅在调用“resume()”或“resumeWithException()”时恢复该协程。


在你的情况下,

override suspend fun doWork(): Result {
return try {
val outputCount = suspendCoroutine<Int> {
val appContext = applicationContext
var mReporter: StepCountReporter?

val mStepCountObserver = object : StepCountReporter.StepCountObserver {
override fun onChanged(count: Int) {
Log.d(APP_TAG, "Step reported : $count")
// This is where the work is completed
it.resume(Result.success(count))
}
}

val mConnectionListener = object : HealthDataStore.ConnectionListener {

override fun onConnected() {
Log.d(APP_TAG, "Health data service is connected.")
mReporter = StepCountReporter(mStore!!)
if (isPermissionAcquired) {
mReporter!!.start(mStepCountObserver)
} else {
Log.e(APP_TAG, "permissions not acquired")
it.resumeWithException(Exception("permissions not acquired"))
}
}

override fun onConnectionFailed(error: HealthConnectionErrorResult) {
Log.d(APP_TAG, "Health data service is not available.")
it.resumeWithException(Exception("Health data service is not available."))
}

override fun onDisconnected() {
Log.d(APP_TAG, "Health data service is disconnected.")
it.resumeWithException(Exception("Health data service is disconnected."))
}
}
mStore = HealthDataStore(appContext, mConnectionListener)
mStore!!.connectService()
// wait for mStepCountObserver.onChanged to be called

}

Result.success()
} catch (error: Throwable) {
Result.failure()
}
}

关于android - 如何使用使用回调的第三方库实现 CoroutineWorker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57129372/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com