0

I have List stored on firebase :-

names : [ "john" , "jack" , "jason" ,
          "jill" , "travis","alice"]

And I have a list :

String[] names = {"john" , "jack" , "jane" , "jason"};

List<String> list = new ArrayList<>(Arrays.asList(names));

How can I find that john , jack and jason exist on firebase and jane doesn't without fetching the list from firebase?

Ahmad
  • 12,336
  • 6
  • 48
  • 88
Krishna Agarwal
  • 131
  • 2
  • 7

1 Answers1

0

The Firebase Database doesn't have an exists operation. So the only way to know if an item exists is to retrieve it.

That said, there are a few choices to handle this situation. But I first want to recommend that you fix your data structure: any time you have an array and try to do a contain-operation on it you should likely have used a set. Instead of repeating myself, I'll refer to my answer here: Firebase query if child of child contains a value. Go read it now... I'll be here when you get back.

Now that you've read about sets vs arrays, you'll see that your data is better stored as:

names : { 
  "john": true,
  "jack":true , 
  "jason": true,
  "jill": true,
  "travis": true,
  "alice": true
}

Much better. :-)

Now back to your question: you'll have to retrieve an item to check if it exists. I can think of two ways to do that: either get the entire list or get each individual item.

You retrieve the entire list and then use DataSnapshot.exists() for the specific items:

ref.child("names").addSingleValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        if (snapshot.child("john").exists() && !snapshot.child("jane").exists()) {
            ...
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "onCancelled", databaseError.toException());
        // ...
    }
});

The alternative is to retrieve the individual items:

ref.child("names/john").addSingleValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        if (snapshot.exists()) {
            ...
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "onCancelled", databaseError.toException());
        // ...
    }
});

Which one works best depends largely on how many names you expect to have in the list.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks... for the fix List looks more structured now. But I have a **list** of **90k-95k** elements on firebase And I don't think retrieving list that huge for checking only 5 elements is a good idea. Also both list on firebase and list at the client end can change. So, I have to retrieve list from firebase whenever any of the list changes. Any suggestions ? – Krishna Agarwal Jan 31 '17 at 12:18
  • With that many names, indeed you're better off not retrieving the entire list. The alternative option should still apply though. – Frank van Puffelen Jan 31 '17 at 15:12