I have to migrate from the deprecated synchronous api FirebaseInstanceId.getInstance().getId(); to the new async firebase api:
FirebaseInstallations.getInstance().getId().addOnCompleteListener( task -> {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}
// Get new Instance ID
String id = task.getResult();
});
My problem is that in my old project, in many different point of my code a method like the following one has being called, and all the called expecting a response synchrounus in-line without callback:
public static String getDeviceId() {
if (deviceId == null) {
initDeviceId();
}
return deviceId;
}
private static void initDeviceId() {
deviceId = FirebaseInstanceId.getInstance().getId();
}
How can migrate the code without rewriting all the project ? I have thought to to edit the above methods in this way:
public static String getDeviceId() {
if (deviceId == null) {
deviceId = workerThread.submit(initDeviceId()).get()
}
return deviceId;
}
private fun initDeviceId():Callable<String?>{
return Callable {
val latch = CountDownLatch(1)
var result:String ?= null
FirebaseInstallations.getInstance().id.addOnCompleteListener{
task -> result = task.result
latch.countDown()
}
latch.await()
result
}
}
but in this way I risk to block mainthread.