使用引用从freebase检索多个日期

时间:2018-04-11 15:30:16

标签: android firebase firebase-realtime-database

我有两类患者和预约

The structure of my database

我需要在特定日期选择所有约会,并为每个约会选择使用患者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;
    }
}

现在我需要执行检索

我尝试了以下代码,但失败了,因为for循环中的每个任务都在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) {

        }
    });

我该如何处理它,还是可以推广数据库结构来解决它?

1 个答案:

答案 0 :(得分:1)

您正在尝试将关系结构应用于firebase数据库,这是一个非关系型数据库。您需要restructure您的数据才能更有效地执行查询。查看此article可帮助您对数据进行非规范化。

不是在每个appointement中保存patient_id,而是应该保存患者本身。

请注意,firebase具有在多个位置的对象中执行updates的工具,因为您可以在多个约会中拥有相同的患者。

您仍然可以保留患者&#34;表&#34;但您在更新患者时需要担心保持数据同步。