Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playground subscription behaviour examples #2850

Merged

Conversation

iv-mexx
Copy link
Contributor

@iv-mexx iv-mexx commented May 3, 2016

These examples illustrate the different behaviours at subscription between Signal and SignalProducer

The Signal example produces the following output:

--- Subscription ---

Subscriber 1 subscribes to the signal
Send value `10` on the signal
Subscriber 1 received 10
Subscriber 2 subscribes to the signal
Send value `20` on the signal
Subscriber 1 received 20
Subscriber 2 received 20

The SignalProducer example produces the following output:

--- Subscription ---

Subscriber 1 subscribes to producer
New subscription, starting operation
Subscriber 1 received 1
Subscriber 1 received 2
Subscriber 2 subscribes to producer
New subscription, starting operation
Subscriber 2 received 1
Subscriber 2 received 2

@iv-mexx iv-mexx changed the title Playground subscription behavior examples Playground subscription behaviour examples May 3, 2016
observer.sendNext(20)
}

/*:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we use the logEvents operator to automate some of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, we could use logEvent to the same effect:

let (signal, observer) = Signal<Int, NoError>.pipe()

print("Subscriber 1 subscribes to the signal")
signal.logEvents(identifier: "Subscriber 1")
print("Send value `10` on the signal")
// subscriber1 will receive the value
observer.sendNext(10)

print("Subscriber 2 subscribes to the signal")
signal.logEvents(identifier: "Subscriber 2")

print("Send value `20` on the signal")
// Notice that now, subscriber1 and subscriber2 will receive the value
observer.sendNext(20)

But I'm not sure if this is better from a documentation point of view.
Having explicit subscribers and explicitly adding them to the signal might be more clear about whats going on (for a beginner).

@RuiAAPeres
Copy link
Member

Makes sense to be explicit (the Playground's goal is to help beginners understanding ReactiveCocoa's behaviour)

@RuiAAPeres RuiAAPeres merged commit 2bf3629 into ReactiveCocoa:master May 3, 2016
@iv-mexx iv-mexx deleted the playgroundSubscriptionExamples branch May 3, 2016 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants