// *** multicast: converts a source observable sequence to a connectable sequence, and broadcasts its transmissions via a specified topic. print("*****multicast*****") let subject = PublishSubject<Any>() subject.subscribe{print("00:\($0)")} .disposed(by: Observable<Any>. Create {(observer) -> Disposable in sleep(2) OnNext (" Requested local ") observer.oncompleted () return Disposables. Create {print(" Destroyed callback ") Publish () netob. subscribe(onNext: {(anything) in print(" subscribe 1:",anything)}). Prompt (by: Subscribe (onNext: {(anything) in print(" Subscribe 2:",anything)}). Disposed (by: disposeBag) _ = netOB.connect()Copy the code

Observables. The publish () will be generated and returns the ConnectableObservableAdapter object, the object storage observables and new: PublishSubject.

When ConnectableObservableAdapter by subscription, generate AnonymousObserver store to subscribe to the callback, and implement PublishSubject subscribe (the observer), The PublishSubject stores the observer.

When ConnectableObservableAdapter call the connect () method, to generate the Connection object, its lazySubject; Call the Subscribe (Connection) method of the Observable.

Connection as an Observer, all goes to the Connection’s on method, which calls the On method of the PublishSubject held by Connection. The PublishSubject iterates through its stored _observers on methods.