Errata for Combine: Asynchronous Programming with Swift 3rd Edition

Creating this topic to catch any typos and bugs in the 3rd Edition of Combine: Asynchronous Programming with Swift.

Chapter 2 URL

Section Hello Future seems to have missed the declaration of the below statement

You also add a subscriptions set in which you’ll store the subscriptions to the future in the example.

This can cause confusion when following the section. Should this be made available in the chapter code snippets ?

On a second note, this code does not seem to produce any output on the playground page

example(of: "Future") {
    var subscriptions: Set<AnyCancellable>? = []
    
    func futureIncrement(
        integer: Int,
        afterDelay delay: TimeInterval) -> Future<Int, Never> {
        
        print("Original 1")
        return Future<Int, Never> { promise in
            print("Original 2")
            DispatchQueue.global().asyncAfter(deadline: .now() + delay) {
                promise(.success(integer + 1))
            }
        }
    }
    
    let future = futureIncrement(integer: 1, afterDelay: 3)
    
    future
        .sink(receiveCompletion: { print($0) },
              receiveValue: { print($0) })
        .store(in: &subscriptions!)
}

I just see

——— Example of: Future ———

Am I missing something to be running this on the playground ?

Stumbled upon the same result, for the sake of curiosity I’ve opened the final playground and surprise this code is commented LOL.
Googled this : “combine future dispatchqueue”

Edit
if the code is written outside the example(of:action:) function everything works as expected, I’m guessing the subscription is released before the async code is executed.

1 Like

Thanks for the response, yes you are right. Another way to make this work is to move the declaration of

var subscriptions: Set<AnyCancellable>? = []

to outside the example(of:action:) function so that this is retained.

Thanks, I’ll have a look - maybe we moved the line during the last edit :+1:t3:

Oh, btw - the link you’ve included in this thread points to the old edition of the book, have you had a look at the update from this year? Combine: Asynchronous Programming with Swift | raywenderlich.com

Thank, yes I did start the earlier version. I am still on that version of XCode :slight_smile:
btw its great to have different versions of the book available for compatibility etc :+1:t2:

Chapter 1 - Operators

I think the middle box should be <String, Error> <String, Never>

hey @omran_k , why do you think so?

@icanzilb shouldn’t an Operator input type be the output of the previous Operator? The first Operator has a output type of <String, Error>

Oh I see, you’re right - they’ve been updating the diagrams recently - this one needs to be fixed. Thanks for spotting this

Hi there,
I’m reading your fantastic book and I think I’ve found a little typo.
On the section I in the chapter 2.1 “Getting started” seems to be written “plauyground” instead “playground”.
Thank you for your work
Pere

Section 8.2

Following piece of code has a typo it seems.

Line of code should be
let newPhotos = selectedPhotosSubject
instead of
let newPhotos = selectedPhotos

1 Like

Thank you very much for reporting this, it seems we’ve introduced this in the last book update when we migrated the project to SwiftUI. There are a couple more places in this chapter where we refer to selectedPhotos - we’re fixing them.

Thanks, we’re fixing this

Hey there, currently I’m doing Chapter 19 and there is an example with testing Combine flatMap and the given code for that is not passing.
Basically, error is that in example expected results should be [1, 2, 4], but then in publisher you are using .flatMap(maxPublishers: .max(2)) and then you are sending 3 subjects to it as in result it only returns result of [1, 2].


It could be that the behavior silently changed since the last book update. @fpillet - did you notice any issues with this code recently?

On Chapter 8: Project “Collage Neue” page 198.

“Open CollageNeueModel.swift, find add(), and replace its body with:”
In the code:
let newPhotos = selectedPhotos

it should said:
let newPhotos = selectedPhotosSubject

Samething happen on the page 206:
let newPhotos = selectedPhotos.shared()

it should said:
let newPhotos = selectedPhotosSubject.share()

Hello,

When I execute this code in Starter.playground, I also don’t get any output. Am I missing something?

var futureSubscriptions = Set<AnyCancellable>()
example(of: "Future") {
    func futureIncrement(
        integer: Int,
        afterDelay delay: TimeInterval) -> Future<Int, Never> {
            Future<Int, Never> { promise in
                //print("Original")
                DispatchQueue.global().asyncAfter(deadline: .now() + delay) {
                    promise(.success(integer + 1))
                }
            }
        }

    let future = futureIncrement(integer: 1, afterDelay: 3)

    future
        .sink(receiveCompletion: { print($0) },
              receiveValue: { print($0) })
        .store(in: &futureSubscriptions)

}

Thank you!