【问题标题】:RxAndroid Subscribe code never called从未调用 RxAndroid 订阅代码
【发布时间】:2017-01-27 11:54:42
【问题描述】:

我对 RxJava 和 RxAndroid 还很陌生,虽然有些东西可以工作,但我现在完全被我认为基本功能不工作的东西所困扰。

我有一个似乎永远不会运行的主题的订阅调用,我不知道为什么:

public class PairManager implements DiscoveryManagerListener {

    private Subscription wifiAvailableSubscription;
    private Subscription debugSubscription;
    private DiscoveryManager discoveryManager;
    private AsyncSubject<Map<String, ConnectableDevice>> availableDevices;

    public PairManager(Context appContext) { 
        DiscoveryManager.init(appContext);
        discoveryManager = DiscoveryManager.getInstance();
        discoveryManager.addListener(this);
        availableDevices = AsyncSubject.<Map<String, ConnectableDevice>> create();

        //
        // This subscription doesn't work
        //
        debugSubscription = availableDevices
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<Map<String, ConnectableDevice>>() {
            @Override
            public void call(Map<String, ConnectableDevice> stringConnectableDeviceMap) {
                //
                // This code is never run !
                //
                Timber.d(">> Available devices changed %s", stringConnectableDeviceMap);
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                Timber.d("Subscription failed %s", throwable);
            }
        });

        availableDevices.onNext(Collections.<String, ConnectableDevice>emptyMap());

        wifiAvailableSubscription = ReactiveNetwork.observeNetworkConnectivity(appContext)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<Connectivity>() {
                @Override
                public void call(Connectivity connectivity) {
                    if (connectivity.getState().equals(NetworkInfo.State.CONNECTED) && connectivity.getType() == ConnectivityManager.TYPE_WIFI) {
                        discoveryManager.start();
                    } else {
                        discoveryManager.stop();
                        availableDevices.onNext(Collections.<String, ConnectableDevice>emptyMap());
                    }
                }
            });
    }

    public AsyncSubject<Map<String, ConnectableDevice>> getAvailableDevices() {
        return availableDevices;
    }

    @Override
    public void onDeviceAdded(DiscoveryManager manager, ConnectableDevice device) {
        Timber.d("onDeviceAdded %s", device);
        availableDevices.onNext(manager.getAllDevices());
        Timber.d("Sanity check %s", availableDevices.getValue());
    }

    // ...

}

有没有办法调试出了什么问题?我已经尝试创建基本的Observable.from-type 调用并记录这些调用,并且按预期工作。 onDeviceAdded 中的健全性检查日志也打印并表明 availableDevices 实际上已按预期更新。我做错了什么?

【问题讨论】:

    标签: android rx-java rx-android


    【解决方案1】:

    我发现了问题,我使用了 AsyncSubjects,它只在它们完成时才发出值,我期望 BehaviorSubjects 的功能。

    【讨论】:

      【解决方案2】:

      来自文档:

      当连接发生变化时,订阅者会收到通知。连接可以更改其状态或类型。

      你说:

      我有一个主题的订阅电话

      主题不会返回最后一个值。我只会在调用onNext 时返回一个值。我假设 Connectivity 永远不会改变,所以它永远不会触发。

      【讨论】:

        猜你喜欢
        • 2019-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多