我已經結束了現在使用延遲處理程序,雖然我會很樂意接受更好的答案。
class PositionListener implements ChildEventListener {
private MyService service;
private final Handler lastChildHandler = new Handler();
private final Runnable lastChildRunnable = new Runnable() {
@Override
public void run() {
service.onModelChanged();
}
};
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildName) {
// Omitted: Handle this child.
// Push the time at which we let the service know the model has
// changed and redraw the view out another 20ms. This means
// lastChildRunnable.run() will never actually run until after
// the last child for a while.
lastChildHandler.removeCallbacks(lastChildRunnable);
lastChildHandler.postDelayed(lastChildRunnable, 20);
}
// Omitted: other overrides
}
也許可以使用[this?](https://www.firebase.com/docs/java-api/javadoc/com/firebase/client/DataSnapshot.html#getChildrenCount--)然後每次保存一個計數器孩子被添加。 –
「......因爲這只是數據傳輸的兩倍。」你確定嗎? – cartant