Synchronized Apollo Requests with Coroutines inside WorkManager
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 DemoWorker(appContext: Context, workerParams: WorkerParameters) : | |
Worker(appContext, workerParams) { | |
override fun doWork(): Result { | |
.... | |
// Synchronous Apollo request | |
val result = runBlocking { | |
try { | |
val response = client.mutate( | |
UpdateUserMutation.builder() | |
.user(userInput) | |
.build() | |
).execute() | |
if (!response.hasErrors()) { | |
// Get response body in response.data() | |
Result.success() | |
} else { | |
Result.failure() | |
} | |
} catch (e: Exception) { | |
Result.retry() | |
} | |
} | |
return result | |
} | |
} |
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
// Coroutines Extension function for making Synchronous ApolloCall's | |
suspend fun <T> ApolloCall<T>.execute() = suspendCoroutine<Response<T>> { cont -> | |
enqueue(object : ApolloCall.Callback<T>() { | |
override fun onResponse(response: Response<T>) { | |
cont.resume(response) | |
} | |
override fun onFailure(e: ApolloException) { | |
cont.resumeWithException(e) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment