Last active
March 31, 2020 17:46
-
-
Save pfmaggi/28c4f55bb89a4bee7eee050fa9a86f8c to your computer and use it in GitHub Desktop.
Code for the Medium article: "Customizing WorkManager"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Copyright 2019 Google LLC. | |
SPDX-License-Identifier: Apache-2.0 */ | |
/** | |
* A Job that refreshes the conference data in the repository (if the app is active) and | |
* in the cache (if the app is not active). | |
*/ | |
class ConferenceDataWorker( | |
ctx: Context, | |
params: WorkerParameters, | |
private val refreshEventDataUseCase: RefreshConferenceDataUseCase | |
) : CoroutineWorker(ctx, params) { | |
override suspend fun doWork(): Result { | |
Timber.i("ConferenceDataService triggering refresh conference data.") | |
return try { | |
refreshEventDataUseCase(Unit) | |
Timber.d("ConferenceDataService finished successfully.") | |
// Finishing indicating this job doesn't need to be rescheduled. | |
Result.success() | |
} catch (e: Exception) { | |
Timber.e("ConferenceDataService failed. It will retry.") | |
// Indicating worker should be retried | |
if (runAttemptCount < MAX_NUMBER_OF_RETRY) { | |
Result.retry() | |
} else { | |
Result.failure() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment