I
I
Ilya Kvant2015-10-29 11:41:09
Java
Ilya Kvant, 2015-10-29 11:41:09

[java-rx] How to make data update when a new element is added?

The code works, but it seems to me that this approach is wrong
I have a database, requests are created through the Observer
Code in the UI

private Action1 subscriber = new Action1<List<SpeedLogDB>>() {
    @Override
    public void call(List<SpeedLogDB> speedLogDBs) {
        setData(speedLogDBs);
    }
};

private void reloadData() {
    subscription = SpeedLogManager.getInstance().observableSpeedLogsForSession(1).subscribe(subscriber);
}

Code in SpeedLogManager class
private PublishSubject<SpeedLogDB> updateDataBase = PublishSubject.create();

public Observable<List<SpeedLogDB>> observableSpeedLogsForSession(final long sessionId) {
        return Observable.create(new Observable.OnSubscribe<List<SpeedLogDB>>() {
            @Override
            public void call(final Subscriber<? super List<SpeedLogDB>> subscriber) {
                SessionDB session = SessionManager.getInstance().getSession(sessionId);
                if (session != null) {
                    subscriber.onNext(session.getSpeedLogList());
                }
                updateDataBase.subscribe(new SimpleSubscriber<SpeedLogDB>() {
                    @Override
                    public void onNext(SpeedLogDB speedLogDB) {
                        SessionDB session = SessionManager.getInstance().getSession(sessionId);
                        if (session != null) {
                            subscriber.onNext(session.getSpeedLogList());
                        }
                    }
                });
            }
        });
    }

When a new element is added, it is called
updateDataBase.onNext(null);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question