Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BehaviourSubject示例的问题 #14

Open
DevinFu opened this issue Sep 19, 2016 · 1 comment
Open

BehaviourSubject示例的问题 #14

DevinFu opened this issue Sep 19, 2016 · 1 comment

Comments

@DevinFu
Copy link
Contributor

DevinFu commented Sep 19, 2016

代码中在演示BehaviourSubject如何使用时, 存在两个问题.

    1. Data类中loadFromNetwork()函数如果因为网络未连接而发生错误, 不会通知界面CacheFragment进行数据更新.
    1. 我尝试通过如下代码修复问题一:
public void loadFromNetwork() {
        Network.getGankApi()
                .getBeauties(100, 1)
                .subscribeOn(Schedulers.io())
                .map(GankBeautyResultToItemsMapper.getInstance())
                .doOnNext(new Action1<List<Item>>() {
                    @Override
                    public void call(List<Item> items) {
                        Database.getInstance().writeItems(items);
                    }
                })
                .subscribe(new Action1<List<Item>>() {
                    @Override
                    public void call(List<Item> items) {
                        cache.onNext(items);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        throwable.printStackTrace();
                        //新增代码: 手动触发cache的错误回调函数
                    cache.onError(throwable);
                    }
                });
    }

修复完成之后发现, 若第一次请求因为网络不可用问题发生错误, 则在网络恢复之后重试请求时, 会直接触发onError函数, 原因是由于cache作为BehaviorSubject, 其中最后一个元素是Throwable. 一旦产生新的订阅, 则立即发送Throwable对象.

@dev4mobile
Copy link

@DevinFu BehaviorSubject 改为 PublicSubject 就不会发生恢复网络请求立即发送Throwable 对象了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants