I want to implement this code synchronously
but job.join
, deferred.await
, and firebase await
, not working.
Does anyone know a solution?
CoroutineScope(Dispatchers.Main).launch {
val job = launch {
Tasks.whenAllComplete(tasks)
.addOnCompleteListener {
Log.d("user-get", "on-0-> addOnComplete")
tasks.forEach { task ->
val snapshot = task.result
Log.d("user-get", "on->"+ task.toString())
snapshot.documents.forEach {
docs.add(it)
}
}
}
}
job.join()
Log.d("user-get", "job-end")
}
Log order:
D/user-get: job-end
D/user-get: on-0-> addOnComplete
D/user-get: on->com.google.android.gms.tasks.zzw@5c0519d
deferred
and await
, and Task.whenAllComplete(tasks).addOnCompleteListener { ~~~ }.await
are also same log result.
really appreciate Alex! the below is the code that I mentioned at comment
override suspend fun checkNickName(nickName: String): Results<Int> {
lateinit var result : Results<Int>
fireStore.collection("database")
.document("user")
.get()
.addOnCompleteListener { document ->
if (document.isSuccessful) {
val list = document.result.data?.get("nickNameList") as List<String>
if (list.contains(nickName))
result = Results.Exist(1)
else
result = Results.No(0)
//document.getResult().get("nickNameList")
}
else {
}
}.await()
return result
}
Is it proper way to use both firebase and coroutine? it was executed as I expected