【问题标题】:Configuring RxJava to Send Data to activity from GCMListenerService配置 RxJava 以从 GCMListenerService 向活动发送数据
【发布时间】:2016-03-11 14:52:47
【问题描述】:

我正在尝试从我的GCMServiceListener 向我的Activity 发送更新,所以我使用RxJava/RxAndroid 并创建了一个BusClass 用于处理发送和Observers

public class ClientBus {

//private final PublishSubject<Object> _bus = PublishSubject.create();

// If multiple threads are going to emit events to this
// then it must be made thread-safe like this instead
private final Subject<Object, Object> _bus = new SerializedSubject<>(PublishSubject.create());

public void send(Object o) {
    _bus.onNext(o);
}

public Observable<Object> toObserverable() {
    return _bus;
}

public boolean hasObservers() {
    return _bus.hasObservers();
}
}

在我的Application Class 中,我这样做是为了初始化BusClass

private ClientBus clientBus;

public ClientBus getRxBusSingleton() {
    if (clientBus == null) {
        clientBus = new ClientBus();
    }
    return clientBus;
}

在我想接收消息的活动中,我注册了一个CompositeSubscription,并从Application Class 获得了对我的ClientBus class 的引用

clientBus = ((MyApplication) getApplicationContext()).getRxBusSingleton();

 @Override
protected void onStart() {
    super.onStart();
    initSubscriptions();
}

@Override
protected void onStop() {
    super.onStop();
    _subscriptions.unsubscribe();
}


void initSubscriptions() {
    _subscriptions = new CompositeSubscription();
    _subscriptions.add(clientBus.toObserverable().subscribe(new Action1<Object>() {
        @Override
        public void call(Object event) {
            Log.e("New Event", "Event Received");
            if (event instanceof MyGcmListenerService.Message) {
                String msg = ((MyGcmListenerService.Message) event).getMessage();
                if (msg.equals("Update Available")) {
                    scheduleArrayList = getSchedules();
                    scheduleAdapter = new ScheduleAdapter(getApplicationContext(), scheduleArrayList, ScheduledUberActivity.this);
                    scheduledList.setAdapter(scheduleAdapter);
                    scheduleAdapter.notifyDataSetChanged();
                } else if (msg.equals("Refresh")) {
                    fetchTrips();
                }
            }
        }
    }));
}

MyGcmListenerService class 收到新通知时我这样做了

 private void sendRefreshNotif() {
    if (clientBus.hasObservers()) {<--It enters the if cause the Log prints. But, the activity doesn't get the message
        Log.e("Obervers", "Observers aren't null");
        clientBus.send(new Message("Refresh"));
    }
}

我不明白为什么它在这里不起作用?我用它在活动和片段之间进行交互。我关闭了我的应用程序以检查通知是否进入,它会进入这个块if (clientBus.hasObservers()) {,但它没有进入并启动应用程序并测试Observer,它注意到有一个活动的观察者。有什么帮助吗?谢谢。

【问题讨论】:

  • 就时间而言,您是在订阅事件总线之后发送消息吗?
  • 消息总是在Subscribing 之后发送,因为我的GCMListener 类是发送消息的那个。而等待信息的Activity在里面注册了SubscriptiononStart
  • 你为什么不使用greenbus或otto?
  • 因为他们做 RxJava 做的事情。而且我一直在我的应用程序的多个场景中使用 RxJava。当一个人可以做到时,为什么将我的应用程序与更多库捆绑在一起。我需要做的就是了解它是如何工作的@Pedram
  • 因为重新发明轮子需要您宝贵的时间。 otto 是一个简单易用的库,它可以完成所有开箱即用的线程工作,而且它只有大约 100 个方法。您可能对尝试实现的工作示例感兴趣:nerds.weddingpartyapp.com/tech/2014/12/24/…

标签: android rx-android


【解决方案1】:

您似乎在CompositeSubscriptionMyApplication 中使用了ClientBus 类的不同实例。 尝试从 ClientBus 类中创建一个单例,它对我来说很好。

public class ClientBus {

    public ClientBus(SingletonAccessor accessor) {}

    private static ClientBus instance;
    private static class SingletonAccessor{}

    public static ClientBus getInstance() {
        if (instance == null) instance = new ClientBus(new SingletonAccessor());
        return instance;
    }

    private final Subject<Object, Object> mBus = new SerializedSubject<>(PublishSubject.create());

    public void send(Object o) {
        mBus.onNext(o);
    }

    public Observable<Object> toObserverable() {
        return mBus;
    }

    public boolean hasObservers() {
        return mBus.hasObservers();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多