在android中使用firebase和geofire获取数据

时间:2017-07-30 19:24:42

标签: android firebase geofire

以下是我的代码。每个代码都可以工作,直到我检查用户是否为女性时,它只会在我的数组和地图中返回1个用户6次。我在swift做了同样的事情并且工作得很好。

public void getNearby(GeoLocation location){       GeoLocation center =新的GeoLocation(location.latitude,location.longitude);

    final GeoQuery nearQuery = geoFire.queryAtLocation(center, 1.6);
    nearQuery.setRadius(1.6);
    nearQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {
            String dbKey = key;
            if (dbKey != null) {
                profileRef.child(key).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(final DataSnapshot dataSnapshot) {
                        String userKey = dataSnapshot.getKey();
                        user3 = dataSnapshot.getValue(User3.class);
                        Log.v("Check", user3.toString());

                        if (userKey.equals(userId)) {
                            Log.v("IDUser", user.getUid());
                        } else if (!userKey.equals(userId)) {
                            Log.v("LogGeo", user3.getUid());


                            database.getReference().child("Blocked").child(userId).child(user3.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot snapshot) {
                                    if (snapshot.exists()) {
                                        Log.v("Snaps", "You blocked everyone!");
                                    } else {
                                         database.getReference().child("users").child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
                                             @Override
                                             public void onDataChange(DataSnapshot dataSnapshot) {
                                                 Map<String, String> cMap = (Map<String, String>) dataSnapshot.getValue();
                                                 String seeking = cMap.get("seeking");

                                                 if (seeking.equals("Woman")) {

                                                     database.getReference().child("users").child(user3.getUid()).addValueEventListener(new ValueEventListener() {
                                                         @Override
                                                         public void onDataChange(DataSnapshot snap) {
                                                             Map<String, String> map = (Map<String, String>) snap.getValue();

                                                             String gender = map.get("gender");
                                                             String age = map.get("age");
                                                             int ageN = Integer.parseInt(age);
                                                             int minAgeN = Integer.parseInt(minAge);
                                                             int maxAgeN = Integer.parseInt(maxAge);
                                                             if (gender.equals("Female")) {
                                                                 if ((ageN >= minAgeN) && (ageN <= maxAgeN)) {
                                                                     user3ArrayList.add(user3);


                                                                 }
                                                             }
                                                         }

                                                         @Override
                                                         public void onCancelled(DatabaseError databaseError) {

                                                         }
                                                     });
                                                 } else if (seeking.equals("Man")) {
                                                     database.getReference().child("users").child(user3.getUid()).addValueEventListener(new ValueEventListener() {
                                                         @Override
                                                         public void onDataChange(DataSnapshot dataSnapshot) {
                                                             Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();
                                                             String gender = map.get("gender");
                                                             String age = map.get("age");
                                                             int ageN = Integer.parseInt(age);
                                                             int minAgeN = Integer.parseInt(minAge);
                                                             int maxAgeN = Integer.parseInt(maxAge);

                                                             if (gender.equals("Male")) {
                                                                 if ((ageN >= minAgeN) && (ageN <= maxAgeN)) {
                                                                     user3ArrayList.add(user3);

                                                                 }
                                                             }
                                                         }

                                                         @Override
                                                         public void onCancelled(DatabaseError databaseError) {

                                                         }
                                                     });
                                                 } else if (seeking.equals("Both")) {
                                                     database.getReference().child("users").child(user3.getUid()).addValueEventListener(new ValueEventListener() {
                                                         @Override
                                                         public void onDataChange(DataSnapshot dataSnapshot) {
                                                             Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();
                                                             String age = map.get("age");
                                                             int ageN = Integer.parseInt(age);
                                                             int minAgeN = Integer.parseInt(minAge);
                                                             int maxAgeN = Integer.parseInt(maxAge);
                                                             if ((ageN >= minAgeN) && (ageN <= maxAgeN)) {
                                                                 user3ArrayList.add(user3);

                                                             }
                                                         }

                                                         @Override
                                                         public void onCancelled(DatabaseError databaseError) {

                                                         }
                                                     });
                                                 }
                                             }

                                             @Override
                                             public void onCancelled(DatabaseError databaseError) {

                                             }
                                         });
                                    }
                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {

                                }
                            });

                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

            }

        }

        @Override
        public void onKeyExited(String key) {

        }

        @Override
        public void onKeyMoved(String key, GeoLocation location) {

        }

        @Override
        public void onGeoQueryReady() {

        }

        @Override
        public void onGeoQueryError(DatabaseError error) {

        }
    });

}

1 个答案:

答案 0 :(得分:0)

这是因为您正在使用嵌套查询。 You are creating a new reference and you adding a new listener every time you iterate.如果您有1位用户6次,则表示您在该节点中有6个孩子,但返回结果只有1次,6次。

为了解决这个问题,只需从第一个查询中获取第二个查询,您的问题就会得到解决。

希望它有所帮助。