Chapter 4 - Alert OnNext subscription

I just worked through chapter 4. I’m confused. The final showMessage function looks like that:

func showMessage(_ title: String, description: String? = nil) {
    alert(title: title, text: description)
      .subscribe(onNext: { [weak self] in
        self?.dismiss(animated: true, completion: nil)

I don’t understand why onNext is there at all. It never gets called.

Also after each time I save and go back and forth to the photosViewController, the resource count will increase by two. Shouldn’t it stay the same?

Thanks for your help!

About the onNext, there’s an error in the text and this is mentioned in the RxSwift v1.0 Errata thread, which you can find here :slight_smile:


Thanks @seantanly!
A follow-up question: Why would I even need to subscribe to the alert when it’s already dismissing from within the Disposeables.create closure in UIAlertViewController+Rx.swift. This one fires when the close button was tapped. So in the example we’re dismissing twice, aren’t we?

@herrdertoene You’re right that dismiss is being called twice if we’re hooking it to both onComplete and as part of Disposable.

However, we still need to call .subscribe(), if not, the Observable returned from showAlert() will not be exercised, i.e. the closure passed to Observable.create is not executed.

The observable created from the Observable.create is termed a Cold Observable covered in later chapters (Chapter 15 p. 294).

Thanks, I’m understanding it better now! :relaxed: