2016-09-23 57 views
0

我搜索城市,如果它不存在,我想做一個與城市存在的情況不同的行爲。RxJava:條件代碼

public void onAddButtonClick(String cityName) { 
     Subscription subscription = repository.getCity(cityName) 
       .filter(city -> city != null) 
       .subscribeOn(backgroundThread) 
       .flatMap(city -> repository.saveCityToDb(city)) 
       .observeOn(mainThread) 
       .subscribe(city -> view.cityExists()); 

     subscriptions.add(subscription); 
} 

getCity()方法:

public Observable<City> getCity(String name){ 
     return fileRepository.getCityFromFile(name); 
    } 

getCityFromFile()

public Observable<City> getCityFromFile(String cityName){ 
     try { 
      InputStream is = assetManager.open(FILE_NAME); 
      Scanner scanner = new Scanner(is); 
      while (scanner.hasNextLine()) { 
       String line = scanner.nextLine(); 
       if (line.toLowerCase().contains(cityName.toLowerCase())) { 
        String[] cityParams = line.split("\t"); 
        City city = new City(); 
        city.setId(Long.parseLong(cityParams[0])); 
        city.setName(cityParams[1]); 
        return Observable.fromCallable(() -> city); 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return Observable.fromCallable(() -> null); 
    } 

所以當城市沒有找到我想要的警報給用戶,並在城市找到我想要進一步(保存到數據庫,打開主屏幕等)。我使用運算符filter(),但它不完全是我想要的,如果城市== null,它就不會更進一步。 你可以請一些更好的主意建議嗎?

回答

2

這取決於你如何設計你的代碼。

如果您搜索一個城市但找不到它,可能會返回一個Observable.empty。或者返回Observable.error而不是(如果是錯誤的話)。然後,如果出現空白/錯誤Observable,則可以使用另一個Observable

例如:

Observable<City> observableIfNoCity = /** observable with perform actions when where is no city */ 
    repository.getCity(wrongCity) // return an Observable.empty if no city 
       .flatMap(city -> repository.saveCityToDb(city)) 
       .doOnNext(city -> view.cityExists()) 
       .switchIfEmpty(observableIfNoCity) 
       .subscribe(); 

如果返回的Observable.error,您可以使用onErrorResumeNext而不是switchIfEmpty

但爲了表現正確,我認爲您應該避免在getCityFromFile中發出null值。使用emptyerror代替

public Observable<City> getCityFromFile(String cityName){ 
     return Observable.defer(() -> { 
     try { 
      InputStream is = assetManager.open(FILE_NAME); 
      Scanner scanner = new Scanner(is); 
      while (scanner.hasNextLine()) { 
       String line = scanner.nextLine(); 
       if (line.toLowerCase().contains(cityName.toLowerCase())) { 
        String[] cityParams = line.split("\t"); 
        City city = new City(); 
        city.setId(Long.parseLong(cityParams[0])); 
        city.setName(cityParams[1]); 
        return Observable.just(city); 
       } 
      } 
     } catch (IOException e) { 
      return Observable.error(e); 
     } 

     return Observable.empty(); // or Observable.error(new NotFoundException()); 
    }); 
} 
+0

什麼時候我把情況view.cityExists()方法中的城市被發現?它會以同樣的方法成爲另一個可觀察對象嗎? – alla

+0

因此,在結果中,我不明白如何使用switchIfEmpty()(我會更詳細地閱讀它),但我喜歡這個想法返回Observable.error(e);或Observable.empty(); ,所以在onAddButtonClick()我在subscribe()方法中添加了以下代碼: 'throwable - > view.showCouldNotFindCity()', '() - > view.showCouldNotFindCity()' – alla

2

你可以使用Observable.error()拋出一個錯誤,並且抓住它在SubsriberonError()方法:

Subscription subscription = Observable.just("String") 
      .flatMap(city -> city == null ? Observable.error(new NullPointerException("City is null")) : Observable.just(city)) 
      .subscribeOn(backgroundThread) 
      .flatMap(city -> repository.saveCityToDb(city)) 
      .observeOn(mainThread) 
      .subscribe(city -> view.cityExists(), 
        throwable -> view.showError());