Android Firestore Query in Kotlin Coroutines (Async without Callback)

Dependencies

dependencies {
    // https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-android
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1'
    // https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-play-services
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.2.1'
}

Usage

launch(Dispatchers.Default) {    val item = firestore.collection("COLLECTION").document("DOCUMENT_ID").get().await()}

or

launch(Dispatchers.Default) {    val d1 = firestore.collection("COLLECTION").document("DOCUMENT_ID").get().asDeferred()    val d2 = firestore.collection("COLLECTION2").document("DOCUMENT_ID2").get().asDeferred()    val (item1, item2) = listOf(d1, d2).awaitAll()}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.