because i want to do nothing when Firebase callbac...
# coroutines
k
because i want to do nothing when Firebase callback is called twice
l
Completion listener should be called only once. What API method is violating that?
k
from crashes i have found that Firestore's
addOnSuccessListeneer
is called twice on some devices
l
I asked which method. Firestore is an API, not a method
k
addOnSuccessListener
is called twice on some devices
l
That's not that generic method I'm asking for, I'm asking for the method returning the
Task
on which you use the
await()
extension function.
k
ooh sorry for misunderstanding,i'm using
get()
something like this
colletion.whereEqualTo("level", level).get(source).await()
l
Can you link that method?
Also, could you show the error message when it's supposedly being called twice?
k
Copy code
Fatal Exception: java.lang.IllegalStateException: Already resumed, but proposed with update kotlin.Unit
kotlinx.coroutines.CancellableContinuationImpl.alreadyResumedError (CancellableContinuationImpl.java:244)
kotlinx.coroutines.CancellableContinuationImpl.resumeImpl (CancellableContinuationImpl.java:239)
kotlinx.coroutines.CancellableContinuationImpl.resumeWith (CancellableContinuationImpl.java:168)
MyClass$loadSomethingSuspended$2$1.onLoaded (MyClassKt.java:94)
l
It's probably that same issue I just opened following me checking the current code: https://github.com/Kotlin/kotlinx.coroutines/issues/1197
👍 1
I mentioned two possible workarounds, these sould fix your issue.
k
thanks @louiscad for your quick help 👍, i think now i only have to use
try/catch
i have another solution as well, but don't know how good it is
Copy code
suspend fun <T> Task<T>.await(): T {
    var continuation: CancellableContinuation<T>? = null
    addOnSuccessListener {
        continuation?.resume(it)
        continuation = null
    }.addOnFailureListener {
        continuation?.resumeWithException(it)
        continuation = null
    }
    return suspendCancellableCoroutine { continuation = it }
}
@louiscad is it safe to use above method?
l
@Kulwinder Singh It'd still crash on post cancellation completation. You need to wrap the last line in a try finally and set the continuation variable back to null.