2017-02-21 94 views
0

因此,我使用這個名爲GeoFire的庫(src:https://github.com/firebase/geofire-java)在一定距離內從用戶處獲取帖子。在我的代碼,我做了這樣的方法:使用GeoQuery檢索數據

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; 
} 

但是這個方法返回一個空HashMap,所以我加了一些日誌,看看到底是怎麼回事,這裏是日誌:

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 

事實證明,我把HashMap返回geoQuery之前發射的日誌。這就是爲什麼該方法返回一個空的原因HashMap

所以,我的問題是,我如何從onKeyEntered內部HashMap這個方法返回?因爲從日誌中可以看到,HashMap已在onKeyPressed內更新,但我無法將其檢索到geoQuery的外部。

注:我在這裏發現了類似的問題Error performing functions inside a GeoFire and Firebase query,但沒有答案。

回答

0

異步調用Geofire回調函數(例如onKeyEntered)......在您從getPostAroundUser方法返回後很可能會調用它。您需要相應地構建您的應用程序。

+0

你能給我一個例子嗎? – YorS

+0

這將取決於您的應用程序以及您想要對數據執行的操作。一個常見的例子可能是更新你的'RecyerlView'適配器,或者從這些回調中更新谷歌地圖。 –

0

你的hashmap爲空的原因,是因爲你已經將它設置爲最終變量,並將其初始化爲一個空集。最後意味着它是不可改變的,因此不能改變。所以,當你初始化它,它是空的,因此將保持空

+0

我的建議是將postHashMapLocation添加爲全局變量而不是本地 – user3918641