2017-12-18 10 views
0

私はRxJavaの初心者であり、一般的なMVP構造です。 Firebase RTDBを伝統的な方法で使用した経験が増えましたが、RemoteDataSource(たとえばTaskRemoteDataSourceなど)として最適な方法を考えています。他のMVPの例では、私はちょうどcallback.onTaskLoaded(タスク)を使用しますが、Flowableの戻り値を必要とする例の契約で使用します。RxJava Firebaseをリモートデータソースとして実装する方法に関するガイダンスが必要

Firebaseのクエリと書き込みのコールバックワールドから来ています。私はRxJavaでFirebaseコールバックを処理するためのベストプラクティスを考えています。 Githubを検索すると、動作するように思われるN個のRxFirebaseライブラリがありますが、私はどのプロジェクトを私のプロジェクトに結びつけるか分かりません。ありがとうございました。

答えて

0

だから、私はRxJavaをFirebase RT1またはFirestoreでラップして使用しています。この場合、Firestoreは次のようになっています。 Firestoreからデータを取得したいとします(このアイデアはRTDとほとんど同じですが、代わりにリアルタイムデータベースを使用してください)。

/** 
    * Retrieves a document snapshot of a particular document within the Firestore database. 
    * @param collection 
    * @param document 
    * @return 
    */ 
    @Override 
    public Observable<DocumentSnapshot> retrieveByDocument$(String collection, String document) { 
     //Here I create the Observable and pass in an emitter as its lambda paramater. 
     //An emitter simply allows me to call onNext, if new data comes in, as well as catch errors or call oncomplete. 
     ///It gives me control over the incoming data. 

     Observable<DocumentSnapshot> observable = Observable.create((ObservableEmitter<DocumentSnapshot> emitter) -> { 
      //Here I wrap the usual way of retrieving data from Firestore, by passing in the collection(i.e table name) and document(i.e item id) and 
     //I add a snapshot listener to listen to changes to that particular location. 

      mFirebaseFirestore.collection(collection).document(document) 
        .addSnapshotListener((DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) -> { 
         if(e != null) { 
        //If there is an error thrown, I emit the error using the emitter 
          emitter.onError(e); 
         }else{ 
        //If there is a value presented by the firestore stream, I use onNext to emit it to the observer, so when I subscribe I receive the incoming stream 
          emitter.onNext(documentSnapshot); 
         } 
        }); 
      }); 
      //I finally return the observable, so that I can subscribe to it later. 
      return observable; 
     } 
関連する問題