rxjava add items after observable was created

Jelly picture Jelly · Mar 7, 2015 · Viewed 11.1k times · Source

I just started using rxjava and I got stuck. Maybe I'm not using rxjava in the right way, but I need to add items to an Observable after it was created. So I understand that You can just call Observable.just("Some", "Items") and the subscribers will receive them, but what if I have an async task and I need to add some more items at later time when the task is finished? I couldn't find anything like Observable.addItems("Some", "More", "Items")

Answer

krp picture krp · Mar 7, 2015

What you probably need is Subject - http://reactivex.io/documentation/subject.html

It is an object that is both Observer and Observable, so you can subscribe to it and emit new items. For example :

PublishSubject<String> subject = PublishSubject.create();
subject.subscribe(System.out::println);
subject.onNext("Item1");
subject.onNext("Item2");