简体   繁体   中英

fetching all uid from firebase database

i am trying to fetch all uid from database and put in a loop to compare all uid Reservations with the reservations that i am making. the point is to compare all time and parking with the new one i am booking so there will no be conflict in reservation.

the firebase strcuture:

在此处输入图像描述

the code

public class MainActivity5 extends AppCompatActivity {
private Button btnMove;
Button b;
RadioGroup radioGroup;
RadioButton selectedRadioButton;
FirebaseDatabase rootnode, rootnode2;
DatabaseReference reference, reference2;
ReservationClass reservationClass;
String str, TimeGroup;
FirebaseAuth auth;
String currentuser = FirebaseAuth.getInstance().getCurrentUser().getUid();
String allUsers = (String) FirebaseAuth.getInstance().getUid();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main5);

    btnMove = findViewById(R.id.ConfirmBooking);
    radioGroup = (RadioGroup) findViewById(R.id.ParkingSpace);

    TimeGroup = getIntent().getExtras().getString("Selected Time : ");


    btnMove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // moveToActivity6();


            selectedRadioButton = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());
            //get RadioButton text
            String parkingSpace = selectedRadioButton.getText().toString();
            final String time3 = TimeGroup;
            final String parking = parkingSpace;
            String id = FirebaseAuth.getInstance().getCurrentUser().getUid();


            /*Intent i = new Intent(MainActivity5.this, MainActivity6.class);
            i.putExtra("Selected Parking space is: ", parkingSpace);
            startActivity(i);*/
            reference2 = FirebaseDatabase.getInstance().getReference("Reservation").child(id);

            reference2.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    if (!snapshot.exists()) {
                        rootnode = FirebaseDatabase.getInstance();
                        reference = rootnode.getReference("Reservation");
                        ReservationClass reservationClass = new ReservationClass(time3, parking);
                        reference.child(currentuser).setValue(reservationClass);
                        Toast.makeText(MainActivity5.this, "Booking successful", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(MainActivity5.this, MainActivity6.class);
                        startActivity(intent);
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });


           /* final FirebaseDatabase database = FirebaseDatabase.getInstance();
            final DatabaseReference myRef = database.getReference("Reservation");

            myRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    if (snapshot.getValue() != null) {
                        for (DataSnapshot ds : snapshot.getChildren()) {
                            String uid = "" + snapshot.getValue();
                             String allUsers = uid;

                        }


                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });*/


            Query query = FirebaseDatabase.getInstance().getReference("Reservation").child(allUsers);
            query.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {

                    selectedRadioButton = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());
                    final String parkingSpace = selectedRadioButton.getText().toString();
                    final String time1 = TimeGroup;
                    final String parking = parkingSpace;
                    final String id = FirebaseAuth.getInstance().getCurrentUser().getUid();


                    for (DataSnapshot snapShot : snapshot.getChildren()) {
                        //Toast.makeText(MainActivity5.this, time1, Toast.LENGTH_SHORT).show();
                        String type = snapshot.child("time").getValue().toString();
                        String type2 = snapshot.child("parking").getValue().toString();
                        if (type.equals(time1) && type2.equals(parking)) {
                            Toast.makeText(MainActivity5.this, allUsers, Toast.LENGTH_SHORT).show();

                        }
                        if (!type.equals(time1) || !type2.equals(parking)) {
                            Toast.makeText(MainActivity5.this, type, Toast.LENGTH_SHORT).show();
                            Toast.makeText(MainActivity5.this, type2, Toast.LENGTH_SHORT).show();
                            rootnode = FirebaseDatabase.getInstance();
                            reference = rootnode.getReference("Reservation");
                            ReservationClass reservationClass = new ReservationClass(time1, parking);
                            reference.child(id).setValue(reservationClass);
                            Intent intent = new Intent(MainActivity5.this, MainActivity6.class);
                            startActivity(intent);


                        }
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });


        }
    });

In your current data structure this requires that you load all reservations and check them individually, meaning the performance linearly depends on the number of reservations in the system.

I highly recommend modifying your (or creating a dedicated) data structure for this, which makes what you want a direct lookup.

Reservations: {
  "A1_0800-0900am": "Q1zT9iCw...OHI1",
  "A4_0800-0900am": "3ReGkVGF...4Cm2",
}

So the keys here are based on the parking spot and time slot, each combination mapping to a idempotent, unique key.

With the above data structure it becomes impossible to add the new reservation for:

  "A1_0800-0900am": "TDBZigbk...7Bs1",

As a reservation for that key already exists.

In fact, it is easy to enforce that existing reservations can't be overwritten in security rules with:

".write": "!data.exists()"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM