February 2020
Intermediate to advanced
412 pages
9h 36m
English
A critical gotcha to note with Subjects is this: the onSubscribe(), onNext(), onError(), and onComplete() calls are not threadsafe! If you have multiple threads calling these four methods, emissions could start to overlap and break the Observable contract, which demands that emissions happen sequentially. If this happens, a good practice to adopt is to call toSerialized() on the Subject object to produce a safely serialized Subject implementation (backed by the private SerializedSubject). This will safely sequentialize concurrent event calls so that no train wreck occurs downstream. The serialization can be done as follows:
Subject<String> subject = PublishSubject.<String>create().toSerialized();
Read now
Unlock full access