Skip to content

Instantly share code, notes, and snippets.

@JEuler
Created February 25, 2019 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JEuler/5ef16e02b1fa420d3a3f899a23d99e28 to your computer and use it in GitHub Desktop.
Save JEuler/5ef16e02b1fa420d3a3f899a23d99e28 to your computer and use it in GitHub Desktop.
SendData Worker example
class SendDataWorker(context: Context, params: WorkerParameters) : Worker(context, params), KoinComponent {
@SuppressLint("CheckResult")
override fun doWork(): Result {
val sendService: SendService? by inject()
val data = inputData.getString("data")
val dataInput = getGsonForJoda()?.fromJson(data, SomeDataClass::class.java)
return try {
val query = sendService?.sendData(dataInput)
?.blockingGet()
if (query == null) {
Result.success()
} else {
Result.retry()
}
} catch (e: IOException) {
Result.retry()
} catch (e: Exception) {
if (e.cause is IOException) {
Result.retry()
} else {
Result.failure()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment