Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use kotlin coroutines in firebase database

I'm trying to access chatting room Using firestore and coroutines.

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

But i get error like this :

E/AndroidRuntime: FATAL EXCEPTION: Timer-0
    Process: com.example.map_fetchuser_trest, PID: 19329
    java.lang.IllegalStateException: Task is not yet complete
        at com.google.android.gms.common.internal.Preconditions.checkState(Unknown Source:29)
        at com.google.android.gms.tasks.zzu.zzb(Unknown Source:121)
        at com.google.android.gms.tasks.zzu.getResult(Unknown Source:12)
        at com.example.map_fetchuser_trest.model.Repository$getOwner$1.invokeSuspend(Repository.kt:53)

How can i get chat document? when i use origin api like below, I can access chat room document.

        firestore.collection("Chat").document(
            "cF7DrENgQ4noWjr3SxKX"
        ).get().addOnCompleteListener { task ->
            if (task.isSuccessful) {
                val chatDTO = task.result?.toObject(Appointment::class.java)
            }
        }
like image 730
ShutUpILoveYou Avatar asked Mar 17 '19 06:03

ShutUpILoveYou


People also ask

What are Kotlin coroutines good for?

Coroutines were added to Kotlin in version 1.3 and are based on established concepts from other languages. On Android, coroutines help to manage long-running tasks that might otherwise block the main thread and cause your app to become unresponsive.

How does Kotlin coroutines work?

A coroutine internally uses a Continuation class to capture the contexts for its execution. Then the dynamic aspect is modeled as a Job class. The use of async usually creates a Deferred job, which is a subclass of the Job class. The CoroutineContext type is required for a coroutine to execute.

How do I launch coroutines?

Launching a Coroutine as a JobThe launch {} function returns a Job object, on which we can block until all the instructions within the coroutine are complete, or else they throw an exception. The actual execution of a coroutine can also be postponed until we need it with a start argument. If we use CoroutineStart.


2 Answers

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.

like image 90
Marko Topolnik Avatar answered Sep 19 '22 12:09

Marko Topolnik


 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"
like image 35
Nurseyit Tursunkulov Avatar answered Sep 19 '22 12:09

Nurseyit Tursunkulov