I am new to Android’s background tasks. I am using Firestore to perform the following tasks:
Read a document. https://firebase.google.com/docs/firestore/query-data/get-data
DBInstance.collection("restaurants") .get() .addOnCompleteListener(task -> { if (task.isSuccessful()) { for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) { // some other code } } else { Log.d(TAG, "Error getting documents: ", task.getException()); } });
Listen to realtime updates of another document. https://firebase.google.com/docs/firestore/query-data/listen
final DocumentReference docRef = DBInstance.collection("users").document(FirebaseAuth.getInstance().getCurrentUser().getUid()); docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() { @Override public void onEvent(@Nullable DocumentSnapshot snapshot, @Nullable FirebaseFirestoreException e) { if (e != null) { Log.w(TAG, "Listen failed.", e); return; } String source = snapshot != null && snapshot.getMetadata().hasPendingWrites() ? "Local" : "Server"; if (snapshot != null && snapshot.exists()) { Log.d(TAG, source + " data: " + snapshot.getData()); // some other code is run } else { Log.i(TAG,"no snapshot found"); } } });
Since these are asynchronous processes, they are performed at the same time (roughly).
I want to trigger an independent method when 1. is completed AND when 2. return a non-null snapshot. Therefore, when some other code
comments above have been completed.
So, I essentially want some background process that sits idle/ listens for the above two conditions and perform a task/call a method that updates certain UI features.
I have briefly read about BroadcastReciever
. Is this relevant? or maybe can I create a custom listener that runs in a background thread? Any suggestions would be helpful since I am not sure what to search for in order to find what I want.
solutions that seems to work (partly suggested by Nehal)
This is the same code as above with the blanks filled in
DBInstance.collection("restaurants")
.get()
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) {
restaurantsLoaded = true;
updateUI();
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
restaurantsLoaded = false;
}
});
final DocumentReference docRef = DBInstance.collection("users").document(FirebaseAuth.getInstance().getCurrentUser().getUid());
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot snapshot,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "Listen failed.", e);
return;
}
String source = snapshot != null && snapshot.getMetadata().hasPendingWrites()
? "Local" : "Server";
if (snapshot != null && snapshot.exists()) {
Log.d(TAG, source + " data: " + snapshot.getData());
usersSnapshotTriggered = true;
udpateUI();
} else {
Log.i(TAG,"no snapshot found");
}
}
});
public void updateUI(){
if(usersSnapshotTriggered && restaurantsLoaded){
// perform the updates
}
}