Reading the topic #2 from this article, it's clear that using create()
properly can involve several issues, like:
Unregister callbacks when an Observable is unsubscribed (failing to do so can cause memory leaks)
Emit events using onNext or onCompleted only while a subscriber is still subscribed
Propagate errors upstream using onError
Handle backpressure
But seeing examples from the devs of RxJava2, like this one, where create()
is used without any further checks or warnings, makes me wonder how deep are the issues with create()
?
If I want Subject
that can push a value at anytime, just like the one in that link:
Subject<Integer> subject = BehaviorSubject.<Integer>create().toSerialized();
Observable<Integer> observable = subject.observeOn(AndroidSchedulers.mainThread());
observable.subscribe(System.out::println);
subject.onNext(1)
Is it ok to use create()
or is it dangerous as well? How to know when we need to protect create()
emissions and when we don't have to?