james
02/08/2021, 12:02 PM# MainActivity.kt
override fun onStart() {
super.onStart()
var applicationID = runBlocking { Application.registerDevice() }
Log.d("AppID on-start", applicationID)
}
# Application.kt
object Application {
suspend fun registerDevice(): String {
return getTokenAsync().await()
}
private suspend fun getTokenAsync(): kotlinx.coroutines.Deferred<String> = coroutineScope {
async {
return@async FirebaseInstallations.getInstance().id.result.toString()
}
}
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.application, PID: 20890
java.lang.IllegalStateException: Task is not yet complete
If anyone could see where I'm going wrong and help point me in the right direction on what I can do to resolve this I would really appreciate it 🙂
Thanks in advance!Ali Albaali
02/08/2021, 2:06 PMjames
02/08/2021, 2:09 PMAli Albaali
02/08/2021, 2:15 PMjames
02/08/2021, 2:18 PMprivate fun fetchID(): String {
var token: String
FirebaseInstallations.getInstance().id
.addOnCompleteListener(OnCompleteListener<String?> { task ->
if (task.isSuccessful) {
token = task.result.toString()
Log.d("App-Token", token)
}
})
return token
}
But token is not initialised and if I initialise it then it won’t use the updated value from the Firebase functionAli Albaali
02/08/2021, 2:20 PMjames
02/08/2021, 2:23 PMfun registerDevice() {
val token = fetchID()
// do other stuff with token
}
How would I go about waiting for it to return the FirebaseInstallations task?james
02/09/2021, 1:36 PM