2017-07-30 68 views
0

下面是我的代碼。每個代碼的作品,直到我過濾掉用戶,當我檢查用戶是女性時,它只返回1個用戶6次在我的數組和地圖。我在swift中做了同樣的事情,它工作得很好。在android中使用firebase和geofire獲取數據

public void getNearby(GeoLocation location){ GeoLocation center = new 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) { 

     } 
    }); 

} 

回答

0

這是發生becase的您正在使用嵌套查詢。 You are creating a new reference and you adding a new listener every time you iterate.如果6次獲得1個用戶,則意味着在該節點中有6個孩子,但返回結果只有一個,6次。

爲了解決這個問題,只需從第一個查詢中取出第二個查詢,問題就會解決。

希望它有幫助。

+0

這仍然不工作我唯一一次得到其他ID是在if(userKey.equals(userId)){ Log.v(「IDUser」,user.getUid()); (!userKey.equals(userId)){ } Log.v(「LogGeo」,user3.getUid()); 之後的任何點只顯示一個ID。 – Rlee128

相關問題