简体   繁体   中英

How to load huge data in firebase recyclerview efficiently

There is something more than 6000 entries in child mDatabase

    mDatabase = FirebaseDatabase.getInstance().getReference().child("CurrentQuizResult");

    Query query = mDatabase.orderByChild("score");

    final FirebaseRecyclerAdapter<BlogScore, AffairsHigh.BlogScoreViewHolder> FirebaseRecyclerAdapter = new FirebaseRecyclerAdapter
            <BlogScore, AffairsHigh.BlogScoreViewHolder>(BlogScore.class, R.layout.blogscore, AffairsHigh.BlogScoreViewHolder.class, query) {


        @Override
        protected void populateViewHolder(final AffairsHigh.BlogScoreViewHolder viewHolder, BlogScore model, final int position) {




            final String post_key = getRef(position).getKey();

            
            });
            viewHolder.setDesc(model.getDesc());
            viewHolder.setUserimage(getApplicationContext(), model.getUserimage());
            viewHolder.setUsername(model.getUsername(),post_key,getApplicationContext());
        }


    };


    mBlogList.setAdapter(FirebaseRecyclerAdapter);

But in this code, it is downloading all 6000 nodes in mDatabase child which are consuming high database usage. Here how I can load data whose score is greater than 200 so that in Query it doesn't load useless data which not required. Any solution for that using Query in firebase??

To only load the data where the score is larger than 200, you can do:

Query query = mDatabase.orderByChild("score").startAt(200);

For more on this, see the Firebase documentation on filtering data .


If the app is still downloading all data, even when you add a filter, you may not have added a server-side index in your database's security rules.

For the query you run, you need to have this index in your rules:

{
  "rules": {
    "CurrentQuizResult": {
      ".indexOn": "score"
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM