Skip to content

Instantly share code, notes, and snippets.

@ferologics
Created November 12, 2017 13:22
Show Gist options
  • Save ferologics/abd76ae1cd2d4de768623c08b9fda8f1 to your computer and use it in GitHub Desktop.
Save ferologics/abd76ae1cd2d4de768623c08b9fda8f1 to your computer and use it in GitHub Desktop.
import Foundation
import RxSwift
// Source - https://medium.com/ios-os-x-development/learn-and-master-%EF%B8%8F-the-basics-of-rxswift-in-10-minutes-818ea6e0a05b
// MARK: - Observable seaquences
let helloSequence = Observable.just("goteeem")
let fibbonachiSequence = Observable.from([0,1,1,2,3,5])
let dictSequence = Observable.from([1:"Got",2:"Em"])
let subscription = helloSequence.subscribe { event in
print(event)
}
let anotherSub = fibbonachiSequence.subscribe { event in
switch event {
case .next(let value): print(value)
case .error(let error): print(error)
case .completed: print("completed")
}
}
func 🚼() {
// create a dispose bag
let bag = DisposeBag()
// create an observable
let observable = Observable.just("GOTEEEM")
// subscribe to observable
let sub = observable.subscribe(onNext: { print($0) })
// dispose of subscription
sub.disposed(by: bag)
}
// MARK: - Subject
func 🚹() {
let bag = DisposeBag()
let subject = PublishSubject<String>()
subject.onNext("Got")
subject.onNext("Em")
// sub 1
_ = subject.subscribe(onNext: {
print($0)
}).disposed(by: bag)
subject.onNext("Goteeeem")
subject.onNext("Goteeeeeeeem")
// sub 2
_ = subject.subscribe(onNext: {
print(#line, $0)
})
subject.onNext("Both of the subs receive this message")
}
// MARK: - MARBLES
/*
Web-App: http://rxmarbles.com
iOS-App: https://itunes.apple.com/com/app/rxmarbles/id1087272442
Android: https://goo.gl/b5YD8K
*/
// MARK: - MARBLES
// MARK: - Transform
func πŸ”Œ() {
// map
_ = Observable<Int>.of(0,1,2,3,4,5)
.map { $0 * 2 }
.subscribe(onNext: {
print($0)
})
// flatMap
let a = Observable<Int>.of(0,1)
let b = Observable<Int>.of(0,1)
let AB = Observable.of(a,b)
_ = AB
.flatMap { return $0 }
.subscribe(onNext: { value in
print(value)
})
// scan
_ = Observable<Int>.of(0,1,2,3,4,5)
.scan(0) { $0 + $1 }
.subscribe(onNext: {
print($0)
})
// buffer
_ = a
.buffer(timeSpan: 150, count: 3, scheduler: MainScheduler.init())
.subscribe(onNext: {
print($0)
})
}
// MARK: - Filter
func 🚬() {
// filter
_ = Observable
.of(2, 30, 90, 200)
.filter { $0 > 10 }
.subscribe(onNext: {
print($0)
})
// distinct until
_ = Observable
.of(1,2,3,1,4,2,3)
.distinctUntilChanged()
.subscribe(onNext: {
print($0)
})
}
// MARK: - Combine
func 🀝() {
// start with
_ = Observable
.of(1,2,3,4,5)
.startWith(2)
.subscribe(onNext: {
print($0)
})
let pub1 = PublishSubject<Int>()
let pub2 = PublishSubject<Int>()
// merge
_ = Observable
.of(pub1, pub2)
.merge()
.subscribe(onNext: {
print($0)
})
pub1.onNext(2)
pub2.onNext(3)
pub1.onNext(4)
// zip - warning: emmits up to a number of elements flowing from any of the composed observables
let a = Observable.of(1,2,3,4,5)
let b = Observable.of("a","b","c","d")
_ = Observable
.zip(a,b) { ($0,$1) }
.subscribe(onNext: {
print($0)
})
// (1, "a")(2, "b") (3, "c") (4, "d")
}
// MARK: Side effects
func πŸ’() {
_ = Observable
.of(0,2,3,4,5)
.do(onNext: {
$0 * 10 // this has no effect on the actual subscription
}).subscribe(onNext: {
print($0)
})
}
// MARK: - Schedulers
func πŸ•§() {
let pub1 = PublishSubject<Int>()
let pub2 = PublishSubject<Int>()
let concurrentScheduler = ConcurrentDispatchQueueScheduler.init(qos: .background)
// observe on concurrent queue and sub on main queue
_ = Observable
.of(pub1, pub2)
.observeOn(concurrentScheduler)
.merge()
.subscribeOn(MainScheduler())
.subscribe(onNext: {
print($0)
})
pub1.onNext(1)
pub2.onNext(2)
}
/*
SOURCES:
https://github.com/ReactiveX/RxSwift/blob/master/Documentation/Schedulers.md
http://reactivex.io/documentation/operators.html
http://rxmarbles.com
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment