Skip to content

Instantly share code, notes, and snippets.

@ibrahimkteish
Last active February 1, 2018 14:01
Show Gist options
  • Save ibrahimkteish/a67819b19057a1e38709fc04684e4d7d to your computer and use it in GitHub Desktop.
Save ibrahimkteish/a67819b19057a1e38709fc04684e4d7d to your computer and use it in GitHub Desktop.
RxSwift BehaviorSubject
//disposeBag from memoy management
let disposeBag = DisposeBag()
//Create our BehaviorSubject with a string type
let subject = BehaviorSubject(value: "Bob")
//New Subscription
subject.subscribe {
print($0)
}
.addDisposableTo(disposeBag)
//emits
subject.on(.Next("Hello"))
subject.onNext("World") //onNext is a Convenience method equivalent to on(.Next(element: E))
//New Subscription
let secondSubscription = subject.subscribe {
print("second subscription:", $0)
}
//add it to the disposeBag
secondSubscription.addDisposableTo(disposeBag)
//emit
subject.onNext("Ibrahim")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment