2016-11-09 2 views
0

RxJavaを使用してAndroidアプリケーションで特定の形式のQRコードをテストしたいと考えています。私はいくつかの条件についてQRコードをチェックする必要があり、それが本当であれば、私はさらにチェックを止め、それらに反応する必要があります。 UIにQRコードが無効であるというエラーメッセージを表示します。RxJavaフィルタを適用してイベントに反応しますか?

私はObservable.errorを使用することをお勧めしません。極端なイベントにのみ使用する必要があるためですが、フィルタリングしているイベントは極端ではありませんが、予期されることがあります。スキャンされたQRコードがアプリケーション用に作成されていないか、QRコードに含まれているデータが無効です。そうでなければ私はこのような何かについて考えているだろう:

Observable.just(barcode) 
       .doOnNext(new Action1<Barcode>() { 
        @Override 
        public void call(Barcode barcode) { 
         if(barcode.rawValue == null) { 
          throw new RuntimeException("empty"); 
         } 
         if(barcode.rawValue == null) { 
          throw new RuntimeException("empty"); 
         } 
        } 
       }) 
       .onErrorResumeNext(new Func1<Throwable, Observable<? extends Barcode>>() { 
        @Override 
        public Observable<? extends Barcode> call(Throwable throwable) { 
         //do something here 
        } 
       }) 
       .subscribe(new Subscriber<Barcode>() { 
        //update UI to show result 
       }); 

onError()にストリームを送信せずにデータのための私のバーコードをテストすることをお勧めでしょうか?

答えて

0

Barcodeスキャン結果を表す別のタイプを導入することができます。すなわち

class BarcodeScanningResult { 
    Barcode barcode; 
    String error; 
    public BarcodeScanningResult(Barcode barcode, String error) { 
     this.barcode = barcode; 
     this.error = error; 
    } 
} 

し、それを使用します。

Observable.just(barcode) 
    .map(new Func1<Barcode, BarcodeScanningResult>() { 
     @Override 
     public BarcodeScanningResult call(Barcode barcode) { 
      if(barcode.rawValue == null) { 
       return new BarcodeScanningResult(barcode, "empty") 
      } 
      if(barcode.rawValue.trim().length == 0) { 
       return new BarcodeScanningResult(barcode, "blank"); 
      } 
      return new BarcodeScanningResult(barcode) 
     } 
    }).subscribe(new Subscriber<BarcodeScanningResult>() { 

}) 
関連する問題