2

enter image description hereI have a list of Keys in a Array List . I want to retrieve data only from those keys but at a same time.

      DatabaseReference mDBRef;

      List<String> keys = new ArrayList<>();

I tried this with loop but the result coming in Model class is repeated 2 times.

            for (int i= 0;i<keys.size();i++)
            {
                String id = keys.get(i);
                Log.d("Keys",id);
                mDBRef = FirebaseDatabase.getInstance().getReference().child("Gyms").child(id);


                mDBRef.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(final DataSnapshot dataSnapshot) {

                        dataSnapshot.getKey();
                         gyms = dataSnapshot.getValue(Gyms.class);


                        if (gyms != null)
                        {
                            Log.d("Names",gyms.getName());

                            Toast.makeText(NearMeActivity.this, ""+ gyms.getName(), Toast.LENGTH_SHORT).show();

                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
Dheeraj Rijhwani
  • 351
  • 5
  • 18
  • can you explain how what are you matching it? you want to read the data or query the data ? – Umar Hussain Apr 27 '18 at 13:09
  • I know about Firebase Cloud FireStore but I want to able to use because I want to use GeoFire in my app. I got a list of keys from GeoFire query and I add those keys to a Arraylist . – Dheeraj Rijhwani Apr 27 '18 at 13:11
  • Please add your database structure. – Alex Mamo Apr 27 '18 at 13:15
  • There's nothing immediately clear from your updated code. If you're seeing the same data twice, it's likely your `onDataChange` is being triggered twice with the same data. Set a breakpoint in there and check for that. If this `onDataChange` only gets triggered once, you might have another listener somewhere else in your app. It's impossible for us to know that with this snippet, which seems fine. If you can't figure it out, have a look at how to [create a minimal, complete, verifiable example](http://stackoverflow.com/help/mcve). – Frank van Puffelen Apr 27 '18 at 21:32

2 Answers2

5

The Firebase Realtime database doesn't support loading items based on an array of values. You'll have to load each item with a separate call.

While this may convolute your code, it is not nearly as slow as most developers think. This is because Firebase pipelines the requests. For a longer explanation of that, see my answer here: http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • But how I can do it in Android the example is of IOS . And Yes I understand how that pipeline request works. – Dheeraj Rijhwani Apr 27 '18 at 13:30
  • You'd do it by looping over the keys and calling `addValueEventListener` or `addListenerForSingleValueEvent` for each individual key (instead of passing the list of keys in as you do now). If you're having problems with that, update your question to show what you've tried. – Frank van Puffelen Apr 27 '18 at 13:52
  • I updated the code with a new issue can you please help. – Dheeraj Rijhwani Apr 27 '18 at 18:17
0

firebase real time database doesn't support multiple matches. You can see Firestore which is NoSQL and provide some flexibility. See Firestore: https://firebase.google.com/docs/firestore/

You have to use custom search solution like Elastic Search.

Umar Hussain
  • 3,461
  • 1
  • 16
  • 38
  • 1
    Firestore currently also doesn't support queries that retrieve multiple items by their key in one call. See https://stackoverflow.com/questions/46721517/google-firestore-how-to-get-document-by-multiple-ids-in-one-round-trip – Frank van Puffelen Apr 27 '18 at 13:17
  • Yes we have to use Elastic Search – Umar Hussain Apr 27 '18 at 13:18