简体   繁体   English

RxJava:在Observable.just之后,它仍然调用Observable.empty()

[英]RxJava: After Observable.just it still calls Observable.empty()

I want to retrieve particular city from a file by its name. 我想从文件名中检索特定城市。 If the city isn't found I return Observable.empty(); 如果找不到该城市,则返回Observable.empty();。 Otherwise I return Observable.just(city); 否则我返回Observable.just(city); Here is the code: 这是代码:

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

        subscriptions.add(subscription);
    }

And method getCity() : 和方法getCity()

public Observable<City> getCity(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(); 
        });
    }

But the problem is when the city is found and it returns Observable.just(city); 但是问题是找到城市并返回Observable.just(city); it goes to return Observable.empty(); return Observable.empty(); I don't know why. 我不知道为什么 So the code () -> view.showCouldNotFindCity() is called anyway. 因此无论如何都会调用代码() -> view.showCouldNotFindCity()

The problem is that you call this () -> view.showCouldNotFindCity() in onCompleted handler. 问题是您在onCompleted处理程序中调用了此()-> view.showCouldNotFindCity()。 If you take a look at the just() method in RxJava you'll see that it calls first onNext and then onCompleted methods on subscriber. 如果看一下RxJava中的just()方法,您会看到它首先在订阅者上调用onNext,然后再调用onCompleted方法。 So when city is found city -> view.cityExists() gets called and then immediately after () -> view.showCouldNotFindCity(). 因此,当找到城市时,将调用city-> view.cityExists(),然后在()-> view.showCouldNotFindCity()之后立即调用。

I would just throw an error if city is not found in your getCity method. 如果在您的getCity方法中找不到城市,我只会抛出一个错误。 Since your onError already calls desired () -> view.showCouldNotFindCity() method and remove it from onCompleted handler. 由于您的onError已经调用了所需的()-> view.showCouldNotFindCity()方法,并将其从onCompleted处理程序中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM