Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
like image 649
Hortensie Avatar asked Feb 26 '17 16:02

Hortensie


1 Answers

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);
like image 197
Marcos Zolnowski Avatar answered Sep 20 '22 03:09

Marcos Zolnowski