0

So, I used this library called GeoFire (src: https://github.com/firebase/geofire-java) to get a post from user within a certain distance. In my code, I made a method like this:

private HashMap<String, GeoLocation> getPostAroundUser(DatabaseReference rootRef) {
    final HashMap<String, GeoLocation> postHashMapLocation = new HashMap<String, GeoLocation>();
    DatabaseReference refPostGeo = rootRef.child("geo").child("posts");
    final GeoFire geoFire = new GeoFire(refPostGeo);
    GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(mLatitude, mLongitude), 3);
    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {
            Log.i(TAG, "Post with key: " + key + " was added to hash with location (lat, long): (" + location.latitude + ", " + location.longitude + ")");
            postHashMapLocation.put(key, location);
            Log.i(TAG, "number of entry after added " + postHashMapLocation.size());
        }

        @Override
        public void onKeyExited(String key) {
            Log.i(TAG, "Post with key: " + key + " was removed");
            postHashMapLocation.remove(key);
        }

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

        }

        @Override
        public void onGeoQueryReady() {
            Log.i(TAG, "number of entry onGeoQueryReady " + postHashMapLocation.size());
        }

        @Override
        public void onGeoQueryError(DatabaseError error) {

        }
    });
    Log.i(TAG, "total entry in function: " + postHashMapLocation.size()); //postHashMapLocation.size() returns 0
    return postHashMapLocation;
}

But this method returns an empty HashMap, so I added some log to see what is going on, here is the log:

02-21 18:09:32.609 15115-15115/com.example.company.app I/ExampleActivity: total entry in function: 0
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -Kd7DPzEZHSdGHqfbmna was added to hash with location (lat, long): (-6.1459048, 106.692038)
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 1
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -Kd7DULwW01S9-haorm9 was added to hash with location (lat, long): (-6.1459048, 106.692038)
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 2
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -Kd7Dt6uvs50qddJHxiA was added to hash with location (lat, long): (-6.1459048, 106.692038)
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 3
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -Kd7EOF41MijwSfrxpX1 was added to hash with location (lat, long): (-6.1459048, 106.692038)
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 4
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -KdB3mJvB0K6nSCtNnoL was added to hash with location (lat, long): (-6.1333898, 106.6861029)
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 5
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -KdB3r13H7y3jiovlLdi was added to hash with location (lat, long): (-6.1333898, 106.6861029)
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 6
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -KdB58ArIOFKHrfEnYpr was added to hash with location (lat, long): (-6.1335054, 106.6910521)
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 7
02-21 18:09:32.869 15115-15115/com.example.company.app I/ExampleActivity: number of entry onGeoQueryReady 7

It turns out the log I put before returning the HashMap fired up before the log inside the geoQuery. That's why the method return an empty HashMap

So, my question is, how do I get the HashMap from inside the onKeyEntered to be returned by this method? Because as you can see from the log, the HashMap was updated inside onKeyPressed but I can't retrieve it to the outside of geoQuery.

Note: I found a similar problem here Error performing functions inside a GeoFire and Firebase query, but there is no answer.

Community
  • 1
  • 1
YorS
  • 25
  • 7

2 Answers2

0

Geofire callbacks (such as onKeyEntered) are invoked asynchronously....more than likely after you've returned from yourgetPostAroundUser method. You'll need to structure your app accordingly.

John O'Reilly
  • 10,000
  • 4
  • 41
  • 63
  • can you give me an example? – YorS Feb 22 '17 at 06:57
  • It will really depend on your application and what you want to do with the data. A common example might be to update your `RecyerlView` adapter or perhaps google map from those callbacks. – John O'Reilly Feb 22 '17 at 09:23
0

the reason your hashmap is empty, is because you have set it as a final variable and thus initialized it as an empty set. final means it is unmuttable and thus can not change. So when you initialized it, it was empty and thus will stay empty