Created
February 25, 2019 17:02
-
-
Save JEuler/5ef16e02b1fa420d3a3f899a23d99e28 to your computer and use it in GitHub Desktop.
SendData Worker example
This file contains 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
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