June 2017
Intermediate to advanced
400 pages
8h 44m
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 Subject to yield a safely serialized Subject implementation (backed by the private SerializedSubject). This will safely sequentialize concurrent event calls so no train wrecks occur downstream:
Subject<String> subject = PublishSubject.<String>create().toSerialized();
Read now
Unlock full access