a little question about the correct approach when dealing with geoQueries and adapters.
I have a list of users keys in a GeoQuery database (with coordinates obviously), and I would like to display them in a ListView when they enter in a certain range.
Here's some code.
usersGeoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
firebaseDatabase.getReference("users").child(key).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
users.add(user);
// if ( ??? )
mAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onKeyExited(String key) {
for(Iterator<User> it = users.iterator(); it.hasNext();){
User user = it.next();
if(user.id == key){
it.remove();
}
}
// if ( ??? )
mAdapter.notifyDataSetChanged();
}
@Override
public void onKeyMoved(String key, GeoLocation location) { }
@Override
public void onGeoQueryReady() {
// this actually triggers before firebase listeners
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
Actually, this works, but, of course, it notifies changes to the adapter everytime a entry it's added to the list. So it's not good for performance.
Any suggestion for a smart approach about this?
Thanks!
The typical approach would be to keep a count of pending items. So every time you get a new key, you increment the counter. Every time you get the corresponding data, you decrement. Once the counter is 0 and onGeoQueryReady
fired, you know you've gotten all the initial items.