首页 > 解决方案 > 是否可以从 GeoFirestore 查询中删除当前用户?

问题描述

如果您要查询 100 公里半径内的用户,您如何将自己(当前用户)排除在查询之外?

因为当 onDocumentMoved 触发时,如果您是唯一一个在输入文档时触发的移动,您将再次获得用户,但您与用户之间的距离不同,因此如果您在 onDocumentEntered 方法中将它们添加到数据库中,您将获得重复的结果. 我解决它的方法是在再次打开应用程序时从数据库中删除所有 usersInRange。我知道这不是完美的解决方案,我正在寻找更好的解决方案。

这是我的代码:

  public void getLocation(Location location){
    ArrayList<String> docMoved = new ArrayList<>();
    listenerRegistration = documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
            if(value != null && value.exists()){
                if (value.get("LocationRange") != null && value.get("AgeRangeFrom") != null && value.get("AgeRangeTo") != null && value.getString("InterestInGender") != null) {
                    locationRange = (double) value.get("LocationRange");
                    ageRangeFrom = (double) value.get("AgeRangeFrom");
                    ageRangeTo = (double) value.get("AgeRangeTo");
                    interestInGender = value.getString("InterestInGender");
                    ArrayList<HashMap<Object, Object>> usersInRange = (ArrayList<HashMap<Object, Object>>) value.get("UsersInRange");

                    geoQuery = geoFirestore.queryAtLocation(new GeoPoint(location.getLatitude(), location.getLongitude()), locationRange);

                    geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
                        @Override
                        public void onDocumentEntered(@NotNull DocumentSnapshot documentSnapshot5, @NotNull GeoPoint geoPoint) {
                            if (documentSnapshot5.get("age") != null && documentSnapshot5.getString("Gender") != null && documentSnapshot5.get("Images") != null && !documentSnapshot5.getId().equals(user.getUid())) {
                                age = (long) documentSnapshot5.get("age");
                                userGender = documentSnapshot5.getString("Gender");
                                userPhotos = (ArrayList<String>) documentSnapshot5.get("Images");

                                Location locationNew = new Location(documentSnapshot5.getId());
                                locationNew.setLatitude(geoPoint.getLatitude());
                                locationNew.setLongitude(geoPoint.getLongitude());

                                distanceM = location.distanceTo(locationNew);
                                distanceKM = (int) Math.round(distanceM / 1000);


                                if (age >= ageRangeFrom && age <= ageRangeTo && interestInGender.equals("Women") && userGender.equals("Woman")) {
                                    db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.arrayUnion(new User(documentSnapshot5.getString("Name"), age, userPhotos.get(0), documentSnapshot5.getId(), distanceKM, null)));
                                } else if (age >= ageRangeFrom && age <= ageRangeTo && interestInGender.equals("Men") && userGender.equals("Man")) {
                                    db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.arrayUnion(new User(documentSnapshot5.getString("Name"), age, userPhotos.get(0), documentSnapshot5.getId(), distanceKM, null)));
                                } else if (age >= ageRangeFrom && age <= ageRangeTo && interestInGender.equals("Everyone")) {
                                    db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.arrayUnion(new User(documentSnapshot5.getString("Name"), age, userPhotos.get(0), documentSnapshot5.getId(), distanceKM, null)));
                                } else {
                                    db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.arrayRemove(new User(documentSnapshot5.getString("Name"), age, userPhotos.get(0), documentSnapshot5.getId(), distanceKM, null)));
                                }
                            }


                        }

                        @Override
                        public void onDocumentExited(@NotNull DocumentSnapshot documentSnapshot1) {
                            db.collection("UsersFilter").document(user.getUid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                                @Override
                                public void onSuccess(DocumentSnapshot documentSnapshot) {
                                    ArrayList<HashMap<Object, Object>> usersInRange = (ArrayList<HashMap<Object, Object>>) documentSnapshot.get("UsersInRange");
                                    if (usersInRange != null && !usersInRange.isEmpty()) {
                                        for (int i = 0; i < usersInRange.size(); i++) {
                                            if (usersInRange.get(i).containsValue(documentSnapshot1.getId())) {
                                                db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.arrayRemove(usersInRange.get(i)));
                                            }
                                        }
                                    }
                                }
                            });
                        }


                        @Override
                        public void onDocumentMoved(@NotNull DocumentSnapshot documentSnapshot6, @NotNull GeoPoint geoPoint) {
                            docMoved.add(documentSnapshot6.getId());
                            if(!documentSnapshot6.getId().equals(user.getUid())) {
                                db.collection("UsersFilter").document(user.getUid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                                    @Override
                                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                                        ArrayList<HashMap<Object, Object>> usersInRange = (ArrayList<HashMap<Object, Object>>) documentSnapshot.get("UsersInRange");
                                        if (usersInRange != null && !usersInRange.isEmpty()) {
                                            for (int i = 0; i < usersInRange.size(); i++) {
                                                if (usersInRange.get(i).containsValue(documentSnapshot6.getId())) {
                                                    db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.arrayRemove(usersInRange.get(i)));
                                                }
                                            }
                                        }
                                    }
                                });
                            }
                        }

                        @Override
                        public void onDocumentChanged(@NotNull DocumentSnapshot documentSnapshot1, @NotNull GeoPoint geoPoint) {
                            if(docMoved.size() == 1 && docMoved.get(0).equals(user.getUid())){
                                db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.delete());
                            }
                            db.collection("UsersFilter").document(user.getUid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                                @Override
                                public void onSuccess(DocumentSnapshot documentSnapshot) {
                                    ArrayList<HashMap<Object, Object>> usersInRange = (ArrayList<HashMap<Object, Object>>) documentSnapshot.get("UsersInRange");
                                    if (usersInRange != null && !usersInRange.isEmpty()) {
                                        for (int i = 0; i < usersInRange.size(); i++) {
                                            if (usersInRange.get(i).containsValue(documentSnapshot1.getId())) {
                                                db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.arrayRemove(usersInRange.get(i)));
                                            }
                                        }
                                    }
                                }
                            });
                        }


                        @Override
                        public void onGeoQueryReady() {

                        }

                        @Override
                        public void onGeoQueryError(@NotNull Exception e) {

                        }
                    });

                }
            }
        }
    });

这是在 ViewPager2 中显示用户的代码:

       listenerRegistration = documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {

            if(value != null && value.exists()) {
                usersInRange = (ArrayList<HashMap<Object, Object>>) value.get("UsersInRange");
                usersInChat = (ArrayList<HashMap<Object, Object>>) value.get("UsersInChat");

                imageUrls.clear();
                viewPager2Adapter.notifyDataSetChanged();

                if (usersInChat != null && !usersInChat.isEmpty() && usersInRange != null && !usersInRange.isEmpty()) {
                    for (int i = 0; i < usersInChat.size(); i++) {
                        for (int k = 0; k < usersInRange.size(); k++) {
                            if (usersInRange.get(k).containsValue(usersInChat.get(i).get("userID"))) {
                                usersInRange.remove(usersInRange.get(k));
                            }
                        }
                    }
                }

                if (usersInRange != null && !usersInRange.isEmpty()) {
                    for (int i = 0; i < usersInRange.size(); i++) {
                        String profileImage = (String) usersInRange.get(i).get("profilePic");
                        String userName = (String) usersInRange.get(i).get("name");
                        long userAge = (long) usersInRange.get(i).get("age");
                        String userID = (String) usersInRange.get(i).get("userID");
                        long km = (long) usersInRange.get(i).get("km");

                        imageUrls.add(new User(userName, userAge, profileImage, userID, km, null));

                        viewPager2Adapter.notifyDataSetChanged();

                    }
                }
            }
        }
    });

标签: androidfirebasegoogle-cloud-firestoregeolocationgeofirestore

解决方案


推荐阅读