Francisco Durdin Garcia
Francisco Durdin Garcia

Reputation: 13327

Check if data exist when using onChildEventListener

Is it possible to check if child data exists over a Firebase DataReference when using observeChildEvent? I'm using onChildEventListener to fill a RecyclerView on my application but during the data change, I show a ProgressBar. The problem appears when there's no data in the current query, so I never receive an event and I can't hide my progressBar.

Is there any way to achieve this without launch a previous observeSingleValueEvent with a limit(1) value to check in the dataSnapshot if there is available data?

Upvotes: 2

Views: 222

Answers (1)

Francisco Durdin Garcia
Francisco Durdin Garcia

Reputation: 13327

I'm using RxFirebase2 to work with Firebase, so I did a method to check if a DatabaseReference have childrens, using it together with RxJava to avoid use OnChildEvents when my reference have no childrens:

  public Single<Boolean> checkIfRefHaveAvailableChild(DatabaseReference databaseReference){
      final Query query = databaseReference.limitToFirst(MIN_RETRIEVE_DATA);
      return RxFirebaseDatabase.observeSingleValueEvent(query)
         .subscribeOn(Schedulers.io())
         .take(1)
         .map(dataSnapshot -> dataSnapshot.hasChildren())
         .single(false);
   }

Upvotes: 1

Related Questions