Configuring RxJava to send data to the action from the GCMListenerService

I'm trying to send an update to mine Activityfrom mine GCMServiceListener, so I use RxJava/RxAndroidAND created BusClassto process the sending andObservers

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();
}
}

And in mine, Application ClassI did this to initializeBusClass

private ClientBus clientBus;

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

In action I want to receive a message, I registered CompositeSubscriptionand get a link to my ClientBus classfromApplication 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();
                }
            }
        }
    }));
}

And from MyGcmListenerService classI did it when I received a new notification

 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, Observer. ? .

+4
1

, ClientBus CompositeSubscription MyApplication. 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();
    }
}
+5

All Articles