Thursday, 9 September 2021

Android Worker doWork() runs into for-loop

I see from Server side that Android calls multiple /test API requests - looks like never-ending for loop. This API is defined only in TestWorker class:

class TestWorker(
    context: Context,
    workerParams: WorkerParameters
) : Worker(context, workerParams) {

    override fun doWork(): Result {
        callApiRequest()
        return Result.success()
    }

    private fun callApiRequest() {
        XUseCase.execute()
            .backgroundToMainThread() 
            .subscribe(
                { ->
                    ..
                },
                {
                    ..
                }
            )
    }    

    companion object {
        const val REPEAT_INTERVAL_IN_HOURS = 3L
    }
}

private fun initTestWorker() {
    val constraints = Constraints.Builder()
        .setRequiredNetworkType(NetworkType.CONNECTED)
        .build()

    val workerRequest = PeriodicWorkRequestBuilder<TestWorker>(TestWorker.REPEAT_INTERVAL_IN_HOURS, TimeUnit.HOURS)
        .setConstraints(constraints)
        .build()

    WorkManager
        .getInstance(this)
        .enqueueUniquePeriodicWork("", ExistingPeriodicWorkPolicy.KEEP, workerRequest)
}

initTestWorker() gets called in MyApp onCreate() method. I'm not sure how, but maybe someone has experienced similar behaviour, from the front end I can not reproduce the issue. Any loop experiences using Worker?



from Android Worker doWork() runs into for-loop

No comments:

Post a Comment