0

Hey all I've seen a number of questions on this but none that seem to answer my particular question. I'm trying to retrieve a list of users based on their distance. My Firebase database structure looks like this:

enter image description here

Within the MainActivity I'd running the following methods to gain a result:

private List<User> generateProfiles(){
        uidList = getUserList();
        Log.d("UidList",String.valueOf(uidList.size()));
           DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
           DatabaseReference phoneNumberRef = rootRef.child("users");
           phoneNumberRef.addValueEventListener(new ValueEventListener() {
               @Override
               public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                   for (DataSnapshot ds : dataSnapshot.getChildren()) {
                       try{
                            User tempUser = ds.getValue(User.class);
                            userList.add(tempUser);
                         }
                       catch (IndexOutOfBoundsException e) {
                           Log.d("Index EXception", e.getMessage());
                       }
                   }
                   Log.d("PRIORUSER", userList.toString());
               }

               @Override
               public void onCancelled(DatabaseError databaseError) {
               }
           });


       return userList;
    }

Which calls this method to get a list of users within the geofire query distance:

private List<String> getUserList(){
        try{
        mFusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                uidList.clear();
                double lattitude = location.getLatitude();
                double longitude = location.getLongitude();
                DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("GeoFire");
                GeoFire fire = new GeoFire(ref);
                GeoQuery geoQuery = fire.queryAtLocation(new GeoLocation(lattitude,longitude),8);
                geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
                    @Override
                    public void onKeyEntered(String key, GeoLocation location) {
                            uidList.add(key);
                            uidList.remove(auth.getUid());
                            Log.d("UserUID",auth.getUid());
                            Log.d("List",uidList.toString());

                    }

                    @Override
                    public void onKeyExited(String key) {
                        uidList.remove(key);
                    }

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

                    }

                    @Override
                    public void onGeoQueryReady() {

                    }

                    @Override
                    public void onGeoQueryError(DatabaseError error) {
                        Log.d("GeoError",error.getMessage());

                    }
                });
            }
        });
//asiudhauisdhuasuidas
        }
        catch(SecurityException e){

        }
        return uidList;
    }

I'm questioning whether my set up is correct in terms of having a seperate geofire dir compared to the users and whether the geofire data should be stored within each individual user.

My problem is that if i then try and loop through the UidList in order to get just the uids that are within the geofire query i either seem to get a null return or a return of all the users.

Does anyone have a working solution? End state I would like a List of user objects.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
James Palfrey
  • 753
  • 6
  • 29
  • Possible duplicate of [How to return dataSnapshot value as a result of a method?](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method) – Alex Mamo Jul 05 '18 at 10:13
  • You cannot return the `userList` object as a result of a method because it will always be `null`. Please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback. – Alex Mamo Jul 05 '18 at 10:15
  • Alex, I'm getting a result from the userList the problem is that i'm getting ALL the users and not just the ones that are held within the UidList. I don't think you understood the question. I return a list of users using the GeoFire query held within the getUserList() method (Successful). This uidList needs to be passed into the generateProfiles() method (successful) I then need to iterate through this list and retrieve the User.class from the Firebase Database reference "users" – James Palfrey Jul 05 '18 at 13:11
  • You are getting the result from the `userList` only inside the `onDataChange()` but when you return it, it is `null`. – Alex Mamo Jul 05 '18 at 14:04

1 Answers1

1

Try the following:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("GeoFire");
ref.orderByChild("lattitude").equalTo(lattitude_here).addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
  String userID=datas.getKey();
    }
 }
 @Override
public void onCancelled(DatabaseError databaseError) {
   }
});

Here the snapshot is at GeoFire, then you add a query and iterate to be able to retrieve the ids according to the result of the query.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134