Android Firestore: Get Multple Documents by Ids

Oct 2, 2019

Technically, Firestore Android/Web API doesn't support get multiple documents by ids, though firestore server/backed API support get multiple docs.

You can simulate get multiple documents api by running multiple get call simultaneously using coroutines.

dependencies {
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.2.1'
}
val COLLECTION_NAME = ...fun getMutipleDocs(ids: List<String>): List<Deferred<DocumentSnapshot>> {    val firestore = FirebaseFirestore.getInstance()    return ids.map { id ->        firestore.collection(COLLECTION_NAME).document(id).get().asDeferred()    }}
launch(Dispatchers.Default) {    val ids = ...    val docs = viewModel.getMutipleDocs(ids).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.