How to improve Flowable<Object> data reading from Firebase db using RxJava 2?
Asked Answered
B

1

6

I have Recycler Viewer that displays data from Fire Base db however initial List contains around 4k elements. I am trying to show only first 15 elements instead of waiting for full list to be loaded however not sure how to do it.

I am trying to take(x) elements via Subscriber however it does not improve reading performance (it still waits for 4k elements from Firebase DB). How to speed up this?

Subscriber - Presenter

@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 - source of Data (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);
Backfire answered 26/2, 2017 at 16:55 Comment(0)
A
3

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

Something like this:

@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);
Appositive answered 15/2, 2018 at 23:49 Comment(2)
But then how would you retrieve documents past the 15 limit? Like say if the user scrolls all the way down in the RecyclerView, it automatically polls Firebase for more data past that pointApproximate
Pagination is a popular topic around here. You can have a look at one of these answersGooseherd

© 2022 - 2024 — McMap. All rights reserved.