简体   繁体   中英

how to check query in firestore?

Image attached for reference of firestore云防火墙

trying to check the query in firestore to know if the user has sent the request or received it and it can perform the task accordingly and change the text of button.

firebaseFirestore.collection("users").document(userid).collection("request").whereEqualTo("userid",otherUserid).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if(task.isSuccessful()){
                List<DocumentSnapshot> document = task.getResult().getDocuments();
                if(document.contains("received")){
                    current_state="req_received";
                    send.setText("Accept Request");
                }else if(document.contains("sent")){
                    current_state = "req_sent";
                    send.setText("Cancel Request");
                }
            }
        }
    });

The following code is to send or cancel the Request and it working fine but when I go back to another activity and come back again then the text of button changes to send request which must have stayed to cancel request as the request has already been sent.

 send.setOnClickListener(new View.OnClickListener() {
                   @Override
        public void onClick(View v) {


            if (userid.equals(otherUserid)) {
                Toast.makeText(ProfileActivity.this, "Cannot send request to your own", Toast.LENGTH_SHORT).show();
                return;
            }
            //--------Not friends.....

            send.setEnabled(false);
            if (current_state.equals("not_friends")) {

                FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
                //  String userid = firebaseAuth.getCurrentUser().getUid();
                Map<String, String> profile = new HashMap<>();
                profile.put("userid", otherUserid);

                firebaseFirestore.collection("users").document(userid).collection("request")
                        .document("sent").set(profile).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
                            //  String userid = firebaseAuth.getCurrentUser().getUid();
                            Map<String, String> profile = new HashMap<>();
                            profile.put("userid", userid);

                            firebaseFirestore.collection("users").document(otherUserid).collection("request")
                                    .document("received").set(profile).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {

                                        send.setEnabled(true);
                                        current_state = "req_sent";
                                        send.setText("Cancel Request");

                                        Toast.makeText(ProfileActivity.this, "Success", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        } else {
                            Toast.makeText(ProfileActivity.this, "Failed sending request", Toast.LENGTH_SHORT).show();

                        }
                    }
                });
            }

            //-------- Cancel request.......

            if (current_state.equals("req_sent")) {
                FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
                Map<String, Object> profile = new HashMap<>();
                //  String userid = firebaseAuth.getCurrentUser().getUid();
                profile.put("userid", FieldValue.delete());

                firebaseFirestore.collection("users").document(userid).collection("request")
                        .document("sent").update(profile).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
                            //  String userid = firebaseAuth.getCurrentUser().getUid();
                            Map<String, Object> profile = new HashMap<>();
                            profile.put("userid", FieldValue.delete());

                            firebaseFirestore.collection("users").document(otherUserid).collection("request")
                                    .document("received").update(profile).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {

                                        send.setEnabled(true);
                                        current_state = "not_friends";
                                        send.setText("Send Request");

                                        Toast.makeText(ProfileActivity.this, "Request canceled", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        } else {
                            Toast.makeText(ProfileActivity.this, "Failed request canceled", Toast.LENGTH_SHORT).show();

                        }
                    }
                });
            }


        }
    });

}

}

As you confirmed, if the error lies only on the comparison part changing the query iteration like below might fix the issue.

if (task.isSuccessful()) {
     for (QueryDocumentSnapshot document : task.getResult()) {
               if(document.getId().contains("received")){
                    current_state="req_received";
                    send.setText("Accept Request");
                    // do pending stuffs or break the loop here
                } else if(document.getId().contains("sent")){
                    current_state = "req_sent";
                    send.setText("Cancel Request");
                    // do pending stuffs or break the loop here
                }
     }
}

ref: https://firebase.google.com/docs/firestore/query-data/queries

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