I have some code executed on an SyncAdapter
on the background thread that looks like this
Task<DocumentSnapshot> docTask = FirebaseFirestore.
getInstance().
collection("users_dummy").
document("ID1").
get();
How can I continue on the background thread when this task has been finished?
I tried to the .addOnCompleteListener
after the get
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>()
{
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task)
{
// DOESN'T WORK, THIS IS THE MAIN THREAD
}
});
but as noted here, the callback is done on the UI or main thread, which actually throws a
java.lang.IllegalStateException: Must not be called on the main application thread
when I perform code like Tasks.await(some task)
Full example here (ignore at will):
private void performTheCodeThatDoesNotWork()
{
Task<DocumentSnapshot> docTask = FirebaseFirestore.getInstance().collection("users_dummy")
.document("ID1").get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>()
{
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task)
{
try
{
// Query the second one but block this time
Task<DocumentSnapshot> secondId = FirebaseFirestore.getInstance().collection("users_dummy")
.document("ID2").get();
/* THIS WILL THROW THE EXCEPTION ->*/ Tasks.await(secondId);
DocumentSnapshot result = secondId.getResult();
}
catch (Exception ex)
{
ex.printStackTrace();
}
Log.i("TAG", "Result is there but never reached");
}
});
Log.i("TAG", "Im here quickly, because async");
}
I've tried Task.continueWith
but with the same results. Do I need to facilitate an Executor and call addOnCompleteListener
with it like in this doc stating leaking activities?
Or am I missing something trivial about this issue? Is it that hard to continue on some thread but perform blocking calls there?