Reputation: 4522
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")
Upvotes: 30
Views: 13537
Reputation: 21
It is useful to note that PublishSubject
does not cache items. For example if the above code was the following, Item1
would not have been printed since the subject was not yet subscribed.
PublishSubject<String> subject = PublishSubject.create();
subject.onNext("Item1");
subject.subscribe(System.out::println);
subject.onNext("Item2");
Use ReplaySubject
for caching.
It would be helpful to read
this
Upvotes: 2
Reputation: 2247
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");
Upvotes: 53