Task API
Task — asynchronous task that returns an error or value in the corresponding callback (onFailure
, onSuccess
).
Below is an implementation example which applies the SDK payment method getProducts()
.
Task
processing example
Async methods return Task<T>
. For example, RuStoreBillingClient.getProducts()
returns Task<ProductsResponse>
. Thus, Task
will return ProductsResponse
if the method appears to be successful. below).
val task: Task<ProductsResponse> = RuStoreBillingClient.products.getProducts()
Add callback OnSuccessListener
to Task
in order to get the successful method result (see below).
val task: Task<ProductsResponse> = RuStoreBillingClient.products.getProducts()
task.addOnSuccessListener {
// Process success
}
To get an execution error, add callback OnFailureListener
to Task
(see below).
val task: Task<ProductsResponse> = RuStoreBillingClient.products.getProducts()
task.addOnFailureListener {
// Process error
}
Add addOnCompletionListener
to Task if you need to get both a successful result and error: below).
If [cause] == null
, then the Task
was completed successfully via Task.TaskResultProvider.setTaskSuccessResult
.
If [cause] == [TaskCancellationException]
, the Task
is terminated via the [Task.cancel]
method, otherwise [Task]
was terminated with an error via [Task.TaskResultProvider.setTaskErrorResult]
val task: Task<ProductsResponse> = RuStoreBillingClient.products.getProducts()
task.addOnCompletionListener{ cause ->
// Process
}
Multi-threading
Callback added to Task
are performed on the main app thread. To run callback in other threads, transfer your Executor
to the callback
method.
Adding executor
via coprocess (see below).
val task: Task<ProductsResponse> = RuStoreBillingClient.products.getProducts()
task.addOnSuccessListener(Dispatchers.IO.asExecutor()) {
// Process success
}
Synchronous execution
You can use task.await()
if your code is already executed in the working thread and you need to get the result synchronously (see below).
try {
val task: Task<ProductsResponse> = RuStoreBillingClient.products.getProducts()
task.await()
} catch (e: CancellationException) {
// Process error
}
The await()
method blocks the thread in which it runs. To avoid ANR, call the await() method in the background thread.
It is recommended to use the await()
method after the following SDK versions:
- In-app payments SDK -
billingclient:1.1.1
; - Push notifications SDK -
pushclient:0.1.8
; - Ratings and reviews SDK -
review:0.1.6
; - Updates SDK -
appupdate:0.1.1
.
Calling the await()
method for SDKs of earlier versions may result in increased battery consumption!
Handling Task API via coprocesses
You can use the following code to process the Task
in a coprocess.
suspend fun <T> Task<T>.wrapInCoroutine(): Result<T> =
suspendCancellableCoroutine { cont ->
cont.invokeOnCancellation { cancel() }
addOnSuccessListener { value ->
if (cont.isActive) {
cont.resume(Result.success(value))
}
}
addOnFailureListener { error ->
if (cont.isActive) {
cont.resume(Result.failure(error))
}
}
}