这是我在
Android应用程序中使用时间戳的firebase数据的简单查询
Query recentStaticJobQuery = reference.child(AppConstants.WORKINDIA_JOBS) .child(AppConstants.WORKINDIA_STATIC_JOBS) .orderByChild(AppConstants.TIMESTAMP) .startAt(lastStaticJobSyncTime); recentStaticJobQuery.addListenerForSingleValueEvent (staticJobDownloadListener); ValueEventListener staticJobDownloadListener = new ValueEventListener() { @Override public void onDataChange(final DataSnapshot dataSnapshot) { Log.i("Firebase","Called") } @Override public void onCancelled(DatabaseError databaseError) { Log.i("Firebase","onCancelled") } };
如何避免onDataChange在android Firebase中被调用两次?
解决方法
使用SingleEventListener而不是ValueEventListener像这样
Firebase ref = new Firebase("YOUR-URL-HERE/PATH/TO/YOUR/STUFF"); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String value = (String) dataSnapshot.getValue(); // do your stuff here with value } @Override public void onCancelled(FirebaseError firebaseError) { } });