I have some RxJava code similar to this but I wonder if the Subject itself in this case could be garbage collected before my subscription triggers in case the caller doesn't hold a reference on the returned observable and just ignores it.
I am also in a spring environment in which I have a task executor that lets me execute logic on a different thread.
public SingleSubject<...> doStuff(final String someParam) {
final SingleSubject<...> observable = SingleSubject.create();
applicationTaskExecutor.execute(() -> {
try {
// Usually here some actually logic would be implemented
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
e.printStackTrace();
}
observable.onSuccess(....);
} catch (Exception e) {
observable.onError(e);
}
});
observable.observeOn(Schedulers.from(applicationTaskExecutor))
.subscribe(... do something else when subject above triggers);
return observable;
}
question from:
https://stackoverflow.com/questions/66052388/rxjava-will-the-observable-be-garbage-collected 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…