Issue
How can I test if a document in firestore exists?
I want to test if a document exists in firestore, I tried this:
mDocRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
Settings activity = activityReference.get();
if (documentSnapshot.exists()) {
usable = false;
Log.d("checkIfUsable", String.valueOf(usable));
} else {
usable = true;
Log.d("checkIfUsable", String.valueOf(usable));
}
}
});
I can see the message in the log, but the variable usable does not get updated in time. The code after this should get executed after finishing testing for the document's existence.
I thought using an AsyncTask might work, but it didn't. Here what I have done:
private static class checkIfUsable extends AsyncTask<String, Void, Void> {
private WeakReference<Settings> activityReference;
// only retain a weak reference to the activity
checkIfUsable(Settings context) {
activityReference = new WeakReference<>(context);
}
private WeakReference<Application> appReference;
checkIfUsable(Application context) {
appReference = new WeakReference<>(context);
}
@Override
protected Void doInBackground(String... strings) {
DocumentReference mDocRef = FirebaseFirestore.getInstance().document("savedSampleData/" + Arrays.toString(strings));
mDocRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
Settings activity = activityReference.get();
if (documentSnapshot.exists()) {
activity.usable = false;
Log.d("checkIfUsable", String.valueOf(activity.usable));
} else {
activity.usable = true;
Log.d("checkIfUsable", String.valueOf(activity.usable));
}
}
});
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Settings activity = activityReference.get();
if (activity.usable) {
final SharedPreferences sharedPreferences = activity.getSharedPreferences(SETTINGS_SHARED_PREF_FILE_KEY, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(PERSONAL_SAMPLE_CODE_KEY, String.valueOf(activity.text));
editor.apply();
activity.update();
} else {
Toast.makeText(appReference.get(), R.string.something_went_wrong_check_internet_or_use_other_code, Toast.LENGTH_LONG).show();
}
}
}
I got this error at the constructor new checkIfUsable().execute();
cannot resolve constructor
Is there another way to check if a document exists? Did I code the AsyncTask wrong?
Thanks for any help
Solution
You said "usable does not get updated in time". It sounds like you're looking for some guarantee on when your callback will get invoked. This is almost certainly a bad idea, because any number of factors can delay the round trip of data between your app and Firestore.
Also, bear in mind that get() method (and all Firestore query operations) are asynchronous. They will return immediately, before any work has been done. This means you are obliged to use your callback (or Task) to determine when the data is available. Any code that depends on your usable
variable needs to be triggered only in response to that callback.
If you want to learn how to make better use the Task objects that are generated by Firebase APIs, read my blog series.
Answered By - Doug Stevenson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.