Reputation: 344
I have two classes Patient and Appointment
I need to select all appointments in a specific date and for each one of the appointments the data of patient using patient id
Patient.java
public class Patient {
private String name;
private String phone;
private String patient_id;
public Patient(){
}
public Patient(String name, String phone, String patient_id) {
this.name = name;
this.phone = phone;
this.patient_id = patient_id;
}
}
Appointment.java
public class Appointment {
@Exclude
public static final int START_HOUR = 8;
@Exclude
public static final int START_MINUTE = 0;
@Exclude
public static final int MAX_NUMBER = 100;
@Exclude
public static final int FIXED_PERIOD = 15;
@Exclude
private Patient patient;
@Exclude
private String date;
private String patient_id;
private String time;
private String color;
public Appointment(){
}
public Appointment(String patient_id, String date, String time, String color) {
this.patient_id = patient_id;
this.date = date;
this.time = time;
this.color = color;
}
}
Now I need to perform retrieving
I have tried the following code, but it failed as every task in the for loop is run in a different thread handled by firebase
mDatabase.child("appointments").child(date).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
appointmentsList = new ArrayList<>();
//Toast.makeText(getActivity(),String.valueOf(dataSnapshot.getChildrenCount()),Toast.LENGTH_LONG).show();
for(DataSnapshot d:dataSnapshot.getChildren()){
String last_time = "";
if(d.getValue().getClass().equals(String.class)){
last_time = d.getValue().toString();
}else {
final Appointment appointment = d.getValue(Appointment.class);
mDatabase.child("patients").child(appointment.getPatient_id()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot2) {
Patient patient = dataSnapshot2.getValue(Patient.class);
appointment.setPatient(patient);
appointmentsList.add(appointment);
}
@Override
public void onCancelled(DatabaseError databaseError2) {
}
});
}
}
mNoteView.setText(getString(R.string.data_for_date, date));
updatePatientsAdapter();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
How can I handle it, or can i promote the database structure to solve it ?
Upvotes: 2
Views: 271
Reputation: 12005
You're trying to apply a relational structure to firebase database, which is a non relational database. You will need to restructure your data in order to perform queries more effectively. Checkout this article to help you denormalize your data.
Instead of saving the patient_id in each appointement you should save the patient itself.
Note that firebase has tools to perform updates in objects in multiple locations exatcly because you can have the same patient in multiple appointments.
You can still keep the patients "table", but you'll need to worry about keeping your data in sync when updating patients.
Upvotes: 1