Android Google Api Get Task Result Without Callback
May 13, 2019Kotlin Corutines Extension for Google Play Services
Tasks.await
Google Api usually return a Task object, when we use addOnCompleteListener
or addOnSuccessListener
to retrieve the desired result.
val fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationClient.lastLocation
.addOnSuccessListener { location : Location? ->
// Got last known location. In some rare situations this can be null.
}
If we are already in a background thread, we might want to retrieve the result without a callback. Use Tasks.await
to get the result synchronously without callback.
val result = Tasks.await(fusedLocationClient.lastLocation)
You might want to handle the following exception
ExecutionException
- The Task failed, this is the same exception you’d get in a non-blocking failure handler.InterruptedException
- An interrupt occurred while waiting for the task to complete.TimeoutException
- if specified a timeout:Tasks.await(task, 500, TimeUnit.MILLISECONDS)
NOTE: When you use Tasks.await
, the real exception is hidden in ExecutionException.cause
(e.g. java.util.concurrent.ExecutionException: com.google.firebase.FirebaseNetworkException: A network error ...
)
Kotlin Coroutines: Task to Deferred
If you are using kotlin coroutines, you can utiliaze kotlinx-coroutines-play-services.
Include dependencies
dependencies {
// https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-play-services
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.2.1'
}
Usage
val result = fusedLocationClient.lastLocation.await()
NOTE: Unlike Tasks.await
, you can handle the real exception without the need to look into ExecutionException.cause
.
NOTE: There is also convinient function to change such as Deferred.asTask and Task.asDeferred.
References
- algo-trading
- algolia
- analytics
- android
- android-ktx
- android-permission
- android-studio
- apps-script
- bash
- binance
- bootstrap
- bootstrapvue
- chartjs
- chrome
- cloud-functions
- coding-interview
- contentresolver
- coroutines
- crashlytics
- crypto
- css
- dagger2
- datastore
- datetime
- docker
- eslint
- firebase
- firebase-auth
- firebase-hosting
- firestore
- firestore-security-rules
- flask
- fontawesome
- fresco
- git
- github
- glide
- godot
- google-app-engine
- google-cloud-storage
- google-colab
- google-drive
- google-maps
- google-places
- google-play
- google-sheets
- gradle
- html
- hugo
- inkscape
- java
- java-time
- javascript
- jetpack-compose
- jetson-nano
- kotlin
- kotlin-serialization
- layout
- lets-encrypt
- lifecycle
- linux
- logging
- lubuntu
- markdown
- mate
- material-design
- matplotlib
- md5
- mongodb
- moshi
- mplfinance
- mysql
- navigation
- nginx
- nodejs
- npm
- nuxtjs
- nvm
- pandas
- payment
- pip
- pwa
- pyenv
- python
- recylerview
- regex
- room
- rxjava
- scoped-storage
- selenium
- social-media
- ssh
- ssl
- static-site-generator
- static-website-hosting
- sublime-text
- ubuntu
- unit-test
- uwsgi
- viewmodel
- viewpager2
- virtualbox
- vue-chartjs
- vue-cli
- vue-router
- vuejs
- vuelidate
- vuepress
- web-development
- web-hosting
- webpack
- windows
- workmanager
- wsl
- yarn