2017-02-26 37 views
6

Tôi có Trình xem Recycler hiển thị dữ liệu từ Fire Base db tuy nhiên Danh sách ban đầu chứa khoảng 4k phần tử. Tôi đang cố gắng để chỉ hiển thị 15 yếu tố đầu tiên thay vì chờ đợi cho danh sách đầy đủ để được nạp tuy nhiên không chắc chắn làm thế nào để làm điều đó.Làm cách nào để cải thiện Flowable <Object> đọc dữ liệu từ Firebase db bằng RxJava 2?

Tôi đang cố gắng lấy (x) các phần tử qua Người đăng ký tuy nhiên nó không cải thiện hiệu suất đọc (nó vẫn chờ phần tử 4k từ DB Firebase). Làm thế nào để tăng tốc độ này?

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()); 
          } 
         } 
      ); 
    } 

Nhà xuất bản - nguồn gốc của dữ liệu (căn cứ hỏa lực 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); 

Trả lời

2

Tôi tin rằng bạn cần phải sử dụng các phương pháp limitToFirst().

Something như thế này:

@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); 
+0

Nhưng sau đó làm thế nào bạn sẽ lấy tài liệu quá khứ 15 giới hạn? Giống như nói nếu người dùng cuộn tất cả các con đường xuống trong RecyclerView, nó sẽ tự động thăm dò Firebase để có thêm dữ liệu qua điểm đó –

+0

Phân trang là một chủ đề phổ biến ở đây. Bạn có thể xem [một trong những câu trả lời này] (https://stackoverflow.com/search?q=firebase+pagination) –

Các vấn đề liên quan