Search code examples
iosswiftcombine

Why can't I get passthrough subjects to work for combine?


Just getting into Combine, and for some reason I can't get passthrough subjects to work. Even though I have copy-pasted examples from multiple different sources, they just won't print anything. I have tried with Publishers and CurrentValueSubjects and they work fine, but with PassThroughSubjects; nope. Here's an example that I have tried:

let mySubject = PassthroughSubject<String, Error>()

mySubject.sink(receiveCompletion: { completion in
        print("-- completion", completion)
    }, receiveValue: { value in
        print("-- value", value)
}).cancel()

mySubject.send("one")
mySubject.send("two")
mySubject.send("three")

This is run in viewDidLoad. What am I doing wrong?

Like I said, I have tried Publishers and CurrentValueSubjects with success:

["one", "two", "three"].publisher
    .sink(receiveValue: { v in
        print("-- hello", v)
}).cancel()

let subject = CurrentValueSubject<String, Error>("Initial Value")

subject.send("Hello")

subject.sink(receiveCompletion: { c in
    print("-- completion", c)
}, receiveValue: { v in
    print("-- value", v)
}).cancel()

Solution

  • The warning that you are seeing that the subscription is unused is a hint to store the token returned by sink like so:

    let mySubject = PassthroughSubject<String, Error>()
    
    let cancellable = mySubject
        .sink(
            receiveCompletion: { completion in
                print("-- completion", completion)
            },
            receiveValue: { value in
                print("-- value", value)
            }
        )
    

    then when you call:

    mySubject.send("one")
    mySubject.send("two")
    mySubject.send("three")
    

    you will see this printed out:

    -- value one
    -- value two
    -- value three
    

    you can cancel the subscription if you are not longer interested in receiving updates:

    cancellable.cancel()
    

    or you can send a completion:

    mySubject.send(completion: .finished)
    

    and then you will see this printed out:

    -- completion finished