Subjects as observers, not observables, page 67

“… is something that can act as both an observable and as an observer. And that something is called a Subject.” - page 67

Perhaps this is explained further along in the book however, as far as I have noticed, in the first eleven chapters it is not explained how or why a person would use a subject as an observer. Subjects are used extensively in these chapters as observables but not as observers. Does a discussion of this come up later in the book?

@scotteg Do you have any feedback about this? Thank you - much appreciated! :]

Hi @evs718, it means that a subject can be subscribed to, i.e., to respond to events such as new elements being emitted in a next event, and it can also receive new events such as a next event with an element, which it then emits to its subscribers. This facilitates having an observable that can receive and emit new events during runtime, vs. creating a finite observable.

let disposeBag = DisposeBag()

let subject = PublishSubject<String>()

    .subscribe {
    .disposed(by: disposeBag)

// 1

// 2

Showing two examples of adding a next event onto the subject…

// 1 - passing an Event<String> to the on(_:) operator
// 2 - using the shorthand syntax, which does the same thing (although technically adds one more cpu cycle)

This prints out…


Hope this helps!

I understand that. I think you didn’t read my question carefully.

This topic was automatically closed after 166 days. New replies are no longer allowed.