CoroutineWorker中的Kotlin协程

时间:2019-05-22 12:13:58

标签: multithreading android-workmanager kotlin-coroutines

我正在使用CoroutineWorker从Room db中获取一些数据并将其发布到服务器,然后再次从db中删除该特定答案。我正在使用存储库模式。

_

输出打印

class SendLocalAnswerToServer(ctx: Context, val params: WorkerParameters) :
    CoroutineWorker(ctx, params) {

var useCaseHandler: UseCaseHandler? = null
var deleteAnswerFromTable: DeleteAnswerFromTable? = null
private var uploadAutoSavedAnswer: UploadAutoSavedAnswer? = null
val TAG:String= "SendLocalAnswerToServer"

override suspend fun doWork(): Result = runBlocking {
    val questionID = params.inputData.getLong("questionID", 0)
    val answerDao = UserDatabase.getDatabase(App.getInstance()).answerDao()
    val answerAPIRepo = NetWorkUtil.provideRetrofit().create(AnswerAPI::class.java)
    uploadAutoSavedAnswer = UploadAutoSavedAnswer(this, answerDao, answerAPIRepo)
    deleteAnswerFromTable = DeleteAnswerFromTable(this, answerDao)
    useCaseHandler = InjectionUtils.provideUseCaseHandler()
    val requestValues = UploadAutoSavedAnswer.RequestValues()
    requestValues.questionID = questionID
    Log.e(TAG, " Main 1")

    launch {
        useCaseHandler!!.execute(uploadAutoSavedAnswer!!, requestValues,
                object :
                        UseCase.UseCaseCallback<UploadAutoSavedAnswer.ResponseValues> {
                    override fun onSuccess(response: UploadAutoSavedAnswer.ResponseValues) {
                                Log.e(TAG, " onSuccess 1")
                                response.observable?.subscribeOn(Schedulers.io())?.observeOn(
                                        AndroidSchedulers.mainThread())!!.subscribeWith(

                                        object :
                                                DisposableObserver<UserAnswerResponseModel>() {
                                            override fun onComplete() {
                                            }

                                            override fun onNext(t: UserAnswerResponseModel) {

                                                Log.e("TAG", " onNext ")
                                                val requestValues = DeleteAnswerFromTable.RequestValues()
                                                requestValues.answerID = t.question.id!!
                                                useCaseHandler?.execute(
                                                        deleteAnswerFromTable!!,
                                                        requestValues,
                                                        object :
                                                                UseCase.UseCaseCallback<DeleteAnswerFromTable.ResponseValues> {
                                                            override fun onSuccess(
                                                                    response: DeleteAnswerFromTable.ResponseValues) {
                                                                Log.e("TAG", " onSuccess 2")
                             // I want to return Result.Success() from here
                                                            }

                                                            override fun onError(
                                                                    throwable: Throwable) {
                            // I want to return Result.Error() from here
                                                            }
                                                        })
                                            }

                                            override fun onError(e: Throwable) {
                                            }
                                        })


                    }

                    override fun onError(throwable: Throwable) {
                    }
                })
    }
    Log.e(TAG, " MAIN 2")
    Result.success()

}
}

如何从上一次成功或返回方法返回值,现在它进入第一个函数 useCaseHandler !!。execute(),该函数进一步启动了一个新线程,因此协程仅从那里返回任何解决方案这样,当完成整个api调用并从表中进一步删除时,只有它应该返回成功

1 个答案:

答案 0 :(得分:1)

当您使用useCaseHandler.execute(...)时,它将在哪个线程上执行请求?问题在于您实际上根本没有使用协程。您正在混合基于回调的API和基于Rx的API(不正确)。

您有2个选项。正确使用协程或使用CountDownLatch