Search code examples
javaandroidfirebasegeofire

GeoFire Firebase Query not able to Store userID in ArrayList


I am using GeoFire to get all users with in a certain radius. When I the addGeoQueryEventListener & then use a for loop to get all the user id, instead of returning the UserID it goes another nest deeper returning g and 1.

private int radius = 40;
private Boolean userFound = false;
private String userLocationID;
private ArrayList<String> mUserIDLocation;
final UserLocation userLocation = new UserLocation();
public void getUserLocation() {

    mUserIDLocation = new ArrayList<String>();

    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling

        return;
    }
    fusedLocationClient.getLastLocation().addOnSuccessListener((Activity) MainActivity.this, new OnSuccessListener<Location>() {

        @Override
        public void onSuccess(Location location) {

            if (location != null) {
                //Toast.makeText(this, "UserLocation " + location.toString(), Toast.LENGTH_SHORT ).show();
                //final Location userLocation = location;
                Log.d(TAG, "onSuccess: UserLocation" + location);
                Log.d(TAG, "onSuccess: UserLocation Latitude " + location.getLatitude());

                String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();
                final DatabaseReference mRef = FirebaseDatabase.getInstance().getReference();

                final GeoFire geoFire = new GeoFire(mRef.child("user_location"));
                geoFire.setLocation(user_id, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
                    @Override
                    public void onComplete(String key, DatabaseError error) {

                    }
                });

                GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(location.getLatitude(), location.getLongitude()), radius);


                geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
                    @Override
                    public void onDataEntered(DataSnapshot dataSnapshot, GeoLocation location) {
                        Log.d(TAG, "onDataEntered: DataSnapshot Key " + dataSnapshot);

                        for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                            mFollowing.add(snapshot.getKey());
                        }

                        getPost();
                        Log.d(TAG, "onDataEntered: mFollowing " + mFollowing);
                        Log.d(TAG, "onDataEntered: mFollowing size " + mFollowing.size());

                    }

                    @Override
                    public void onDataExited(DataSnapshot dataSnapshot) {

                    }

                    @Override
                    public void onDataMoved(DataSnapshot dataSnapshot, GeoLocation location) {

                    }

                    @Override
                    public void onDataChanged(DataSnapshot dataSnapshot, GeoLocation location) {

                    }

                    @Override
                    public void onGeoQueryReady() {

                    }

                    @Override
                    public void onGeoQueryError(DatabaseError error) {

                    }
                });
            }
        }
    });

}

Log for database snapshot:

onDataEntered: DataSnapshot Key DataSnapshot { key = xjfXxJ3spuPNywtHyqg5rNnlIMD3, value = {.priority=c295tcm4kd, l={0=48.435, 1=-122.084}, g=c295tcm4kd} }

onDataEntered: DataSnapshot Key DataSnapshot { key = TMOb5NL8igZovGkiZdVcl3UQmxV2, value = {.priority=c295myvnsd, l={0=48.4319983, 1=-122.084}, g=c295myvnsd} }

Log for what is add to mFollowing ArrayList: onDataEntered: mFollowing [g, l] onDataEntered: mFollowing [g, l, g, l]

Log for size of mFollowing ArrayList: onDataEntered: mFollowing size 2 onDataEntered: mFollowing size 4

A picture of my database: enter image description here

What I expect to happen is the userID are added to the mFollowing and when I run the log for mFollowing, I only see 2 userID in the array and the count to be 2.


Solution

  • I figure out the problem. OnDataEntered() is a loop that checks all users in the area. So you store all the users in one Arraylist and then pass them to another array list in OnGeoQueryReady()

    Below is the correct code.

        private int radius = 40;
        private Boolean userFound = false;
        private String userLocationID;
        private ArrayList<String> mUserIDLocation;
        final UserLocation userLocation = new UserLocation();
        public void getUserLocation() {
            final DatabaseReference mRef = FirebaseDatabase.getInstance().getReference();
            final GeoFire geoFire = new GeoFire(mRef.child("user_location"));
            mUserIDLocation = new ArrayList<String>();
    
            swipe.setRefreshing(false);
    
            if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
    
                return;
            }
            fusedLocationClient.getLastLocation().addOnSuccessListener((Activity) MainActivity.this, new OnSuccessListener<Location>() {
    
                @Override
                public void onSuccess(Location location) {
    
                    if (location != null) {
                        //Toast.makeText(this, "UserLocation " + location.toString(), Toast.LENGTH_SHORT ).show();
                        //final Location userLocation = location;
                        Log.d(TAG, "onSuccess: UserLocation" + location);
                        Log.d(TAG, "onSuccess: UserLocation Latitude " + location.getLatitude());
    
                        String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();
    
    
    
    
                        geoFire.setLocation(user_id, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
                            @Override
                            public void onComplete(String key, DatabaseError error) {
    
                            }
                        });
    
                        GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(location.getLatitude(), location.getLongitude()), radius);
    
    
                        geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
                            @Override
                            public void onDataEntered(DataSnapshot dataSnapshot, GeoLocation location) {
    
                                Log.d(TAG, "onDataEntered: datasnapshot " + dataSnapshot);
    
                                mUserIDLocation.add(dataSnapshot.getKey());
    
    
                            }
    
                            @Override
                            public void onDataExited(DataSnapshot dataSnapshot) {
    
                            }
    
                            @Override
                            public void onDataMoved(DataSnapshot dataSnapshot, GeoLocation location) {
    
                            }
    
                            @Override
                            public void onDataChanged(DataSnapshot dataSnapshot, GeoLocation location) {
    
                            }
    
                            @Override
                            public void onGeoQueryReady() {
    
                                mFollowing = mUserIDLocation;
    
                                Log.d(TAG, "onGeoQueryReady: mFollowing users " + mFollowing);
    
                                getPost();
                            }
    
                            @Override
                            public void onGeoQueryError(DatabaseError error) {
    
                            }
                        });
                    }
                }
            });
    
        }
    

    Now the array size will be correct when mFollowing is passed to getPost()