繁体   English   中英

从 firebase 数据库中获取所有 uid

[英]fetching all uid from firebase database

我正在尝试从数据库中获取所有 uid 并放入一个循环中以将所有 uid 预订与我正在进行的预订进行比较。 关键是将所有时间和停车位与我预订的新停车位进行比较,这样预订时就不会发生冲突。

firebase 结构:

在此处输入图像描述

编码

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) {

                }
            });


        }
    });

在您当前的数据结构中,这要求您加载所有预留并单独检查它们,这意味着性能线性取决于系统中的预留数量。

我强烈建议为此修改您的(或创建专用的)数据结构,这使您想要直接查找。

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

所以这里的键是基于停车位和时隙的,每个组合映射到一个幂等的、唯一的键。

使用上述数据结构,不可能为以下内容添加新预留:

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

由于该密钥的保留已经存在。

事实上,很容易强制现有的保留不能在安全规则中被覆盖:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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