Reactive Programming in iOS with Combine, Episode 9: Challenge: Create a Phone Number Lookup | Kodeco, the new raywenderlich.com

In this hands-on challenge use your knowledge of transforming operators to lookup a phone number in a contact book.


This is a companion discussion topic for the original entry at https://www.kodeco.com/5429795-reactive-programming-in-ios-with-combine/lessons/9

Hello Josh,

Thanks for a great course! Quick Question:

With this code in place:

let alice = Chatter(name: "Alice", message: "Hi! I'm Alice!")

let chat:CurrentValueSubject<Chatter,Never> = .init(alice)
chat
    .flatMap { $0.message }
    .sink(receiveValue: {print($0)})
    .store(in: &subscriptions)
 
//Sending a new value to chat publisher
chat.value = alice

alice.message.value = "How are you?" //"How are you?" is printed twice now
//In fact any change to alice.message publisher (which itself is a current value subject) prints the message twice. 

Since the sink closure is only subscribed to the message property of alice, shouldn’t the sink subscriber print the change on message property, only once?

Here’s the console output:

Hi! I’m Alice! // I can understand this is the initial value of alice.message
Hi! I’m Alice! // Update to the chat subject is emitting a new event which get passed down stream to the sink subscriber.

How are you?
How are you? //Not sure what is causing this to print. Does the flat map also emit any changes to the alice.message?

“How are you?” should only be printed once, no?