Android Firestore: Get Multple Documents by Ids

October 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()
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.