So im using coroutine on my project, and im trying to get a firestore Document using coroutine await function. And after i got the document, the handler was called even when the job was success. Anyone can explain to me how to fix it?
Here is my code :
val handler = CoroutineExceptionHandler { _, exception ->
CoroutineScope(Dispatchers.Main).launch {
Log.e(TAG, "saveData: $exception")
saveData(mActivity)
}
}
CoroutineScope(Dispatchers.IO + handler).launch {
val document = Firebase.firestore.collection("Users").document(getUID())
.get().await()
Log.d(TAG, "saveData: $document")
withContext(Dispatchers.Main) {
val currentUser = TemplateUser(
getUID(),
document.data!!["userName"] as String,
document.data!!["userNIS"] as String,
document.data!!["userEmail"] as String,
document.data!!["userKelas"] as String,
document.data!!["kuisSatu"] as Long,
document.data!!["kuisSatuAttempt"] as Long,
document.data!!["kuisDua"] as Long,
document.data!!["kuisDuaAttempt"] as Long,
document.data!!["kuisTiga"] as Long,
document.data!!["kuisTigaAttempt"] as Long,
document.data!!["kuisEmpat"] as Long,
document.data!!["kuisEmpatAttempt"] as Long,
document.data!!["kuisLima"] as Long,
document.data!!["kuisLimaAttempt"] as Long
)
Log.d(TAG, "saveData: $currentUser")
val sharedPreferences =
mActivity.getSharedPreferences("userProfile", Context.MODE_PRIVATE).edit()
with(sharedPreferences) {
putString("userName", currentUser.userName)
putString("userEmail", currentUser.userEmail)
putString("userNIS", currentUser.userNIS)
putString("userKelas", currentUser.userKelas)
putLong("kuisSatu", currentUser.kuisSatu)
putLong("kuisDua", currentUser.kuisDua)
putLong("kuisTiga", currentUser.kuisTiga)
putLong("kuisEmpat", currentUser.kuisEmpat)
putLong("kuisLima", currentUser.kuisLima)
putLong("kuisSatuAttempt", currentUser.kuisSatuAttempt)
putLong("kuisDuaAttempt", currentUser.kuisDuaAttempt)
putLong("kuisTigaAttempt", currentUser.kuisTigaAttempt)
putLong("kuisEmpatAttempt", currentUser.kuisEmpatAttempt)
putLong("kuisLimaAttempt", currentUser.kuisLimaAttempt)
apply()
}
Log.d(TAG, "saveData: Sharepref berhasil diisi dengan data $document")
GreetUser.greetUser(mActivity)
}
}