How to use kotlin coroutines in firebase database

Task is the thing one awaits on, but you wrapped it in another layer of async. Remove the async:

fun getOwner() {
    runBlocking {
        var de =  firestore.collection("Chat").document("cF7DrENgQ4noWjr3SxKX").get()
        var result = de.await().result
    }
}

However, by using runBlocking() you have shot yourself in the foot and wrote blocking code that just formally uses an async API to no good effect.

To truly benefit from it, you must have a

suspend fun getOwner() = firestore
     .collection("Chat")
     .document("cF7DrENgQ4noWjr3SxKX")
     .get()
     .await()
     .result

and launch a coroutine at the place you call it from:

launch {
    val owner = getOwner()
    // update the GUI
}

This assumes you're calling launch from an object that is a CoroutineScope.


 val db = FirebaseFirestore.getInstance()
override suspend fun saveBinToDB(bin: Bin): Result<Unit> {
    lateinit var result:Result<Unit>
    db.collection("bins")
        .add(bin)
        .addOnSuccessListener { documentReference ->
            Log.d(TAG, "DocumentSnapshot written with ID: ${documentReference.id}")
            result = Result.Success(Unit)
        }
        .addOnFailureListener { e ->
            Log.w(TAG, "Error adding document", e)
            result = Result.Error(Exception())
        }
        .await()
    return result
}

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.3.7"