Firestore notice everything has loaded - java

My Firestore Database structure looks like this:
...a Collection with Routine Objects.
...a Collection with Workout Objects. With the attributes
-> RoutineKey: Stores the Key of the Routine which the Workout is from
-> ExerciseEntryKeys: ArrayList<String> of the Keys of the ExerciseEntry from the Workout
...a Collection with ExerciseEntries Objects.
Now I want to load every Workout from a Routine and the ExerciseEntries of a Workout. To do this, I do the following after I have loaded a Routine Object.
for (final DocumentSnapshot doc : documentSnapshots.getDocuments()) {
final WorkoutSNR workout = doc.toObject(WorkoutSNR.class);
workout.setKey(doc.getId());
workoutsFromRoutine.add(workout);
fm.getColRefExerciseEntries().whereEqualTo("workoutKey", workout.getKey()).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot documentSnapshots) {
if (documentSnapshots.isEmpty()) {
prg.setVisibility(View.GONE);
processData();
} else {
for (int i = 0; i < workout.getExcersiseEntryKeys().size(); i++) {
fm.getDocRefExerciseEntrie(workout.getExcersiseEntryKeys().get(i)).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
final ExcersiseEntrySNR entry = documentSnapshot.toObject(ExcersiseEntrySNR.class);
entry.setKey(documentSnapshot.getId());
workout.getExcersises().add(entry);
processData();
prg.setVisibility(View.GONE);
Collections.sort(workout.getExcersises(), new Comparator<ExcersiseEntrySNR>() {
#Override
public int compare(ExcersiseEntrySNR e1, ExcersiseEntrySNR e2) {
if (e1.getPosition() < e2.getPosition()) {
return -1;
} else if (e1.getPosition() > e2.getPosition()) {
return 1;
} else {
return 0;
}
}
});
}
});
}
}
}
});
}
}
});
This works like it should but as you can see I call:
processData();
prg.setVisibility(View.GONE);
Collections.sort(workout.getExcersises(), new Comparator<ExcersiseEntrySNR>() {
#Override
public int compare(ExcersiseEntrySNR e1, ExcersiseEntrySNR e2) {
if (e1.getPosition() < e2.getPosition()) {
return -1;
} else if (e1.getPosition() > e2.getPosition()) {
return 1;
} else {
return 0;
}
}
});
Evertime an ExerciseEntry has been successfully loaded. This is very unnecessary and I want to call this code only once everything(Every ExerciseEnry for every Workout of an Routine).
What is the best way to notice everything has been loaded? Does Firestore provide any function for this?
I have tried having an Integer that counts the number of successful ExerciseLoads and Workout loads but I can only access final variables inside a nested class(Is that how its called?).

How do I know when the data is completely loaded from the database?
You can add a flag to each Routine and Workout objects with the value of false and once you have downloaded those objects, to set the value to true but this is not how things are working with Firestore. You cannot know when an object from the database is completed downloaded becase Cloud Firestore is also a realtime database and getting data might never complete. That's why is named a realtime database because in any momemnt the data under those Routine and Workout objects can be changed, properties can be added or deleted.
You can use a CompletionListener only when you write or update data and you'll be notified when the operation has been acknowledged by the Database servers but you cannot use this interface when reading data.

So if anyone is wondering what my Solution at the end is, here is my current Code:
fm.getColRefWorkoutSNR().whereEqualTo("routineKey", routineKey).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot documentSnapshots) {
final int workoutSize = documentSnapshots.getDocuments().size();
for (final DocumentSnapshot doc : documentSnapshots.getDocuments()) {
final WorkoutSNR workout = doc.toObject(WorkoutSNR.class);
workout.setKey(doc.getId());
workoutsFromRoutine.add(workout);
fm.getColRefExerciseEntries().whereEqualTo("workoutKey", workout.getKey()).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot documentSnapshots) {
if (documentSnapshots.isEmpty()) {
prg.setVisibility(View.GONE);
processData();
} else {
if (workout.getExcersiseEntryKeys().size() > 0) {
for (int i = 0; i < workout.getExcersiseEntryKeys().size(); i++) {
fm.getDocRefExerciseEntrie(workout.getExcersiseEntryKeys().get(i)).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
final ExcersiseEntrySNR entry = documentSnapshot.toObject(ExcersiseEntrySNR.class);
entry.setKey(documentSnapshot.getId());
workout.getExcersises().add(entry);
if (workout.getExcersises().size() == workout.getExcersiseEntryKeys().size()) {
increaseFullyLoadedWorkouts();
}
if (fullyLoadedWorkouts == workoutSize) {
processData();
prg.setVisibility(View.GONE);
}
}
});
}
} else {
increaseFullyLoadedWorkouts();
if (fullyLoadedWorkouts == workoutSize) {
processData();
prg.setVisibility(View.GONE);
}
}
}
}
});
}
}
});
As you can see, I check if I have loaded every exercise for an workout and if thats the case I increase a "fullyLoadedWorkout" counter. Then I check if the counter equals the workout size and if thats the case I know that I have "fully" loaded my data.
I know thats not a good way to do this but its the only solution I can imagine at the moment. This seems to be way easier in the Realtime Database and I'm still consider switching back to it. Any suggestions for a better way are welcomed.

Related

Array call returns null although I have updated data [duplicate]

Before adding a new data into the firestore, i want to check already a data of the same kind exists in the database or not.if already a data was present means i want to prevent the user from entering duplicate data in the database.
In my case it is like a appointment booking if already a booking for the same time exists,i want to prevent to users to book on the same time.i tried using query function but it is not preventing duplicate data entering.someone plz help me
private boolean alreadyBooked(final String boname, final String bodept, final String botime) {
final int[] flag = {0};
CollectionReference cref=db.collection("bookingdetails");
Query q1=cref.whereEqualTo("time",botime).whereEqualTo("dept",bodept);
q1.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (DocumentSnapshot ds : queryDocumentSnapshots) {
String rname, rdept, rtime;
rname = ds.getString("name");
rdept = ds.getString("dept");
rtime = ds.getString("time");
if (rdept.equals(botime)) {
if (rtime.equals(botime)) {
flag[0] = 1;
return;
}
}
}
}
});
if(flag[0]==1){
return true;
}
else {
return false;
}
}
Loading data from Cloud Firestore happens asynchronously. By the time you return from alreadyBooked, the data hasn't loaded yet, onSuccess hasn't run yet, and flag still has its default value.
The easiest way to see this is with a few log statements:
private boolean alreadyBooked(final String boname, final String bodept, final String botime) {
CollectionReference cref=db.collection("bookingdetails");
Query q1=cref.whereEqualTo("time",botime).whereEqualTo("dept",bodept);
System.out.println("Starting listener");
q1.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
System.out.println("Got data from Firestore");
}
});
System.out.println("Returning");
}
If you run this code it will print:
Starting listener
Returning
Got data from Firestore
That's probably not the order you expected. But it perfectly explains why you always get false when calling alreadyBooked: the data simply didn't come back from Firestore in time.
The solution for this is to change the way you think about the problem. Your current code has logic: "First check if it is already booked, then add a new item". We need to reframe this as: "Start checking if it is already booked. Once we know that is isn't, add a new item." In code this means that all code that needs data from Firestore must be inside the onSuccess or must be called from there.
The simplest version is to move the code into onSuccess:
private void alreadyBooked(final String boname, final String bodept, final String botime) {
CollectionReference cref=db.collection("bookingdetails");
Query q1=cref.whereEqualTo("time",botime).whereEqualTo("dept",bodept);
q1.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
boolean isExisting = false
for (DocumentSnapshot ds : queryDocumentSnapshots) {
String rname, rdept, rtime;
rname = ds.getString("name");
rdept = ds.getString("dept");
rtime = ds.getString("time");
if (rdept.equals(botime)) {
if (rtime.equals(botime)) {
isExisting = true;
}
}
}
if (!isExisting) {
// TODO: add item to Firestore
}
}
});
}
While this is simple, it makes alreadyBooked less reusable since now it contains the code to insert the new item too. You can solve this by defining your own callback interface:
public interface AlreadyBookedCallback {
void onCallback(boolean isAlreadyBooked);
}
private void alreadyBooked(final String boname, final String bodept, final String botime, AlreadyBookedCallback callback) {
CollectionReference cref=db.collection("bookingdetails");
Query q1=cref.whereEqualTo("time",botime).whereEqualTo("dept",bodept);
q1.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (DocumentSnapshot ds : queryDocumentSnapshots) {
String rname, rdept, rtime;
rname = ds.getString("name");
rdept = ds.getString("dept");
rtime = ds.getString("time");
if (rdept.equals(botime)) {
if (rtime.equals(botime)) {
isExisting = true;
}
}
}
callback.onCallback(isExisting)
}
});
}
And then you call it as:
alreadyBooked(boname, bodept, botime, new AlreadyBookedCallback() {
#Override
public void onCallback(boolean isAlreadyBooked) {
// TODO: insert item
}
});
Also see (many of these are for the Firebase Realtime Database, where the same logic applies):
getContactsFromFirebase() method return an empty list
Doug's blog post on asynchronous callbacks
Setting Singleton property value in Firebase Listener
Android + Firebase: synchronous for into an asynchronous function
Is it possible to synchronously load data from Firebase?
Querying data from firebase

Wait for Firestore queries to complete

I am currently trying to run multiple queries in firestore and want to wait for them to all complete before executing the next code. I've read up on several possible avenues but I have yet to find a good Android example.
public HashMap<String,Boolean> multipleQueries(String collection, String field, final ArrayList<String> criteria) {
HashMap<String,Boolean> resultMap = new HashMap<>();
for (int i = 0; i < criteria.size(); i++){
final int index = i;
db.collection(collection).whereEqualTo(field,criteria.get(i)).limit(1)
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()) {
if(task.getResult().size() != 0 ){
resultMap.put(criteria.get(index),true);
} else {
resultMap.put(criteria.get(index),false);
}
} else {
resultMap.put(criteria.get(index),false);
}
}
});
}
return resultMap;
}
Since get() returns a Task, you can use Tasks.whenAll(...).
But you won't be able to return a List from this function, since all data is asynchronously loaded. The best you can do is return the result from Task.whenAll(...), which is itself a Task. See Doug Stevenson's great article on becoming a task master: https://firebase.googleblog.com/2016/10/become-a-firebase-taskmaster-part-4.html

firebase dataBase read issue

I'm trying to get value from the last node on firebase Datasebase
I've seen a few solutions but none of them worked for me.
The code is given below:
setData = FirebaseDatabase.getInstance().getReference().child("Questions");
Query lastQuery = setData.orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("JSON_OBJ")) {
SubmitKey item = dataSnapshot.getValue(SubmitKey.class);
String key2 = item.getJSON_OBJ();
} else {
Snackbar.make(findViewById(R.id.QuestionLayout), "JSON_ObJ not found", Snackbar.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
My Constructor Class
public class SubmitKey {
private String JSON_OBJ;
public SubmitKey() {
}
public SubmitKey(String JSON_OBJ) {
this.JSON_OBJ = JSON_OBJ;
}
public String getJSON_OBJ() {
return JSON_OBJ;
}
}
instead of getting the value it is going to the else statement and showing 'JSON_OBJ not Found'.
Any help would be appreciated.
Edit: I've made some changes in the code and now it is showing no setter/field found for 0-101 found on class submitKey
D/ViewRootImpl#5b51511[Questions]: ViewPostIme pointer 0
D/ViewRootImpl#5b51511[Questions]: ViewPostIme pointer 1
And it is aslo not going inside if statement goes to else statement
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
Your onDataChange will need to take care of this list, by iterating over snapshot.getChildren():
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot: snapshot.getChildren()) {
if (dataSnapshot.hasChild("JSON_OBJ")) {
SubmitKey item = dataSnapshot.getValue(SubmitKey.class);
String key2 = item.getJSON_OBJ();
} else {
Snackbar.make(findViewById(R.id.QuestionLayout), "JSON_ObJ not found", Snackbar.LENGTH_LONG).show();
}
}
}

Check if ID exists in Firebase Android

I am writing an android app and I want to check if a key exists in order to avoid duplicate values. I´ve been investigating but it looks that all I can add is listeners, when I just want to check if an ID exists or not already.
Taking this SO question as an example, I would like to know if -JlvccKbEAyoLL9dc9_v exists. How can I do this?
Thanks in advance.
The approach will always be similar to what I wrote in this answer about JavaScript: Test if a data exist in Firebase
ref.child("-JlvccKbEAyoLL9dc9_v").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()) {
// TODO: handle the case where the data already exists
}
else {
// TODO: handle the case where the data does not yet exist
}
}
#Override
public void onCancelled(FirebaseError firebaseError) { }
});
But keep in mind that push ids in Firebase exist to prevent having to do this sort of check. When multiple clients generate push ids, they are statistically guaranteed to be unique. So there's no way one of them can create the same key as another.
Any case where you need to check if an item already exists is likely to have race conditions: if two clients perform this check almost at the same time, neither of them will find a value.
RxJava 2 :
public static Observable<Boolean> observeExistsSingle(final DatabaseReference ref) {
return Observable.create(emitter ->
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
emitter.onNext(dataSnapshot.exists());
emitter.onComplete();
}
#Override
public void onCancelled(DatabaseError databaseError) {
emitter.onError(databaseError.toException());
}
}));
}
Usage:
public Observable<Boolean> isYourObjectExists(String uid) {
return observeExistsSingle(databaseReference.child(uid));
}
In your class:
yourRepo.isYourObjectExists("-JlvccKbEAyoLL9dc9_v")
.subscribe(isExists -> {}, Throwable::printStackTrace);
Based on #Frank van Puffelen's answer, here's a few lines to see if a ref - itself - exists before using it.
public void saveIfRefIsAbsent(DatabaseReference firebaseRef) {
DatabaseReference parentRef = firebaseRef.getParent();
String refString = firebaseRef.toString();
int lastSlashIndex = refString.lastIndexOf('/');
String refKey = refString.substring(lastSlashIndex + 1);
parentRef.child(refKey).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()) {
// TODO: handle the case where the data already exists
}
else {
// TODO: handle the case where the data does not yet exist
}
}
#Override
public void onCancelled(FirebaseError firebaseError) { }
});
}
In my case I have a Util to create the schema programmatically. I use this in order to add new data without overwriting existing data.

Limiting number of selected rows in Vaadin Table

In one of our applications we use the lazy query container to browse a potentially very large dataset. This works great. However, it is possible to select an arbitrary number of rows when using a multi-select table.
In our case, this can lead to selecting up to 500.000 rows (Vaadin limit) and then crash the VM.
Is there a way to limit the number of selected rows?
Here is a sample that shows the problem:
public class UIImpl extends UI {
private int SIZE = 500000;
#Override
protected void init(VaadinRequest request) {
// add a large table
LazyQueryContainer lqc = new LazyQueryContainer(
new QueryFactory() {
public Query constructQuery(QueryDefinition qd) {
return new Query() {
#Override
public int size() {
return SIZE;
}
#Override
public void saveItems(List<Item> addedItems, List<Item> modifiedItems, List<Item> removedItems) { }
#Override
public List<Item> loadItems(int startIndex, int count) {
List<Item> r = new ArrayList<>(count);
for (int i = startIndex; i<startIndex+count;i++) {
PropertysetItem item = new PropertysetItem();
item.addItemProperty("name", new ObjectProperty(i));
r.add(item);
}
return r;
}
#Override
public boolean deleteAllItems() {
return false;
}
#Override
public Item constructItem() {
return null;
}
};
}
},
null,
20,
false
);
lqc.addContainerProperty("name", Integer.class, null);
Table table = new Table();
table.setContainerDataSource(lqc);
table.setMultiSelect(true);
table.setSelectable(true);
table.setImmediate(true);
table.setVisibleColumns("name");
table.setSizeFull();
table.addValueChangeListener(new Property.ValueChangeListener() {
public void valueChange(Property.ValueChangeEvent event) {
System.err.println(event.getProperty().getValue());
}
});
setContent(table);
}
}
If you want to limit the number of rows a user is able to select you can use something similar to the following code:
public class TableWithSelectionLimit extends Table {
private final int maxSelections= -1;
private String[] lastSelected;
public TableWithSelectionLimit(int maxSelections) {
this.maxSelections = maxSelections;
}
#Override
public void changeVariables(Object source, Map<String, Object> variables) {
String[] selected = (String[]) variables.get("selected");
if (selected != null && selected.length > maxSelections) {
if (lastSelected != null) {
variables.put("selected", lastSelected);
} else {
variables.remove("selected");
}
markAsDirty();
} else {
lastSelected = selected;
}
super.changeVariables(source, variables);
}
}
This is of course optimizable, but it gives you an idea on how you could do it.
Update
For handling also selections produced using "Shift"+Click one has to handle/update these selection ranges additionally inside the method mentioned above.
Those can be retrieved using variables.get("selectedRanges") that will return a String[] containing items like "8-10" whereas the
first number is: the start index of the selection range
second number is: the amount of items selected starting at this index
Using this information it should be possible to update those values as wished and put them back into the variables using variables.put("selectedRanges", updatedRanges).
Attention: do not forget to call markAsDirty() if the values are changed, as otherwise the changes won't be propagated to the client side.

Categories