How to improve the current reading of <Object> data from Firebase db using RxJava 2?

I have a Recycler Viewer that displays data from Fire Base db, but the source list contains about 4k items. I am trying to show only the first 15 elements instead of waiting for the full list to load, but am not sure how to do this.

I am trying to take (x) elements through Subscriber, however it does not improve read performance (it is still waiting for 4k elements from Firebase DB). How to speed it up?

Subscriber - Lead

@Override public void onBindViewHolder(final ListContentFragment.ViewHolder holder, int position) { modelInterface.getDataFromFireBase("FinalSymbols") .take(15) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<DataSnapshot>() { @Override public void accept(DataSnapshot dataFromDb) throws Exception { //update TextView inside Recycler Viewer holder.name.setText(dataFromDb.child(String.valueOf(holder.getAdapterPosition())).child("description").getValue().toString()); holder.description.setText(dataFromDb.child(String.valueOf(holder.getAdapterPosition())).child("categoryName").getValue().toString()); } } ); } 

Publisher - Data Source (FireBase db)

 @Override public Flowable<DataSnapshot> getDataFromFireBase(final String childName) { return Flowable.create(new FlowableOnSubscribe<DataSnapshot>() { @Override public void subscribe(final FlowableEmitter<DataSnapshot> e) throws Exception { databaseReference.child(childName).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { e.onNext(dataSnapshot); e.onComplete(); } @Override public void onCancelled(DatabaseError databaseError) { } }); } }, BackpressureStrategy.BUFFER); 
+6
source share
1 answer

I believe that you need to use the limitToFirst() method.

Something like that:

 @Override public Flowable<DataSnapshot> getDataFromFireBase(final String childName) { return Flowable.create(new FlowableOnSubscribe<DataSnapshot>() { @Override public void subscribe(final FlowableEmitter<DataSnapshot> e) throws Exception { databaseReference.child(childName).limitToFirst(15).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { e.onNext(dataSnapshot); e.onComplete(); } @Override public void onCancelled(DatabaseError databaseError) { } }); } }, BackpressureStrategy.BUFFER); 
+2
source

Source: https://habr.com/ru/post/1015303/


All Articles