Wednesday 23 September 2020

Coroutine JobCancellationException on updating Firestore document

Upon calling my ViewModel's saveUser(), the Firebase Firestore document is updated successfully, but the coroutine Job gets cancelled, catching a JobCancellationException, and the log "User #${user.id} saved !" is never printed. Where does this cancellation comes from and how can it complete instead ?

// ViewModel.kt

fun saveUser(user: User) {
    viewModelScope.launch(Dispatchers.IO) {
        Repository.saveUser(user)
        Log.d("test", "User #${user.id} saved !")
    }
}

// Repository.kt

suspend fun saveUser(user: User) {
    val documentReference = db
        .collection(USERS_COLLECTION).document(user.id)

    try {
        documentReference.set(user).await()
        Log.d("test", "Good")
    } catch (e: Exception) {
        Log.e("test", "Not good") // catches a JobCancellationException
    }
}


from Coroutine JobCancellationException on updating Firestore document

No comments:

Post a Comment