저번 포스팅에선 Observable과 Observer 모든 역할이 가능한 Relay와 Subject에 대해 알아보았습니다.
못봤다면 여기 눌러주세요❗️
이번 시간에 Observable을 생성할 수 있는 연산자들에 대해 알아보겠습니다~!
let disposeBag = DisposeBag()
let element = "😀"
Observable.just(element)
.subscribe { event in print(event) }
.disposed(by: disposeBag)
Observable.just([1, 2, 3])
.subscribe { event in print(event) }
.disposed(by: disposeBag)
=========================================
결과
next(😀)
completed
next([1, 2, 3])
completed
let disposeBag = DisposeBag()
let apple = "🍏"
let orange = "🍊"
let kiwi = "🥝"
Observable.of(apple, orange, kiwi)
.subscribe { event in print(event) }
.disposed(by: disposeBag)
Observable.of([1, 2], [3, 4], [5, 6])
.subscribe { event in print(event) }
.disposed(by: disposeBag)
=========================================
결과
next(🍏)
next(🍊)
next(🥝)
completed
next([1, 2])
next([3, 4])
next([5, 6])
completed
let disposeBag = DisposeBag()
let fruits = ["🍏", "🍎", "🍋", "🍓", "🍇"]
Observable.from(fruits)
.subscribe { event in print(event) }
.disposed(by: disposeBag)
=========================================
결과
next(🍏)
next(🍎)
next(🍋)
next(🍓)
next(🍇)
completed
Observable.range(start: 1, count: 10)
.subscribe { print($0) }
.disposed(by: disposeBag)
=========================================
결과
next(1)
next(2)
next(3)
next(4)
next(5)
next(6)
next(7)
next(8)
next(9)
next(10)
completed
Observable.generate(initialState: 0, condition: { $0 <= 10 }, iterate: { $0 + 2 })
.subscribe { print($0) }
.disposed(by: disposeBag)
=========================================
결과
next(0)
next(2)
next(4)
next(6)
next(8)
next(10)
completed
let disposeBag = DisposeBag()
let element = "❤️"
Observable.repeatElement(element)
.take(7) // 7개만 받음
.subscribe { print($0) }
.disposed(by: disposeBag)
=========================================
결과
next(❤️)
next(❤️)
next(❤️)
next(❤️)
next(❤️)
next(❤️)
next(❤️)
completed
let disposeBag = DisposeBag()
let animals = ["🐶", "🐱", "🐹", "🐰", "🦊", "🐻", "🐯"]
let fruits = ["🍎", "🍐", "🍋", "🍇", "🍈", "🍓", "🍑"]
var flag = true
let factory: Observable<String> = Observable.deferred {
flag.toggle()
if flag {
return Observable.from(animals)
} else {
return Observable.from(fruits)
}
}
factory
.subscribe { print($0) }
.disposed(by: disposeBag)
factory
.subscribe { print($0) }
.disposed(by: disposeBag)
factory
.subscribe { print($0) }
.disposed(by: disposeBag)
=========================================
결과
next(🍎)
next(🍐)
next(🍋)
next(🍇)
next(🍈)
next(🍓)
next(🍑)
completed
next(🐶)
next(🐱)
next(🐹)
next(🐰)
next(🦊)
next(🐻)
next(🐯)
completed
next(🍎)
next(🍐)
next(🍋)
next(🍇)
next(🍈)
next(🍓)
next(🍑)
completed
// create에서 요소를 방출할 때는 onNext() 사용
let disposeBag = DisposeBag()
enum MyError: Error {
case error
}
Observable<String>.create { (observer) -> Disposable in
guard let url = URL(string: "https://www.apple.com") else {
observer.onError(MyError.error)
return Disposables.create()
}
guard let html = try? String(contentsOf: url, encoding: .utf8) else {
observer.onError(MyError.error)
return Disposables.create()
}
observer.onNext(html)
observer.onCompleted()
observer.onNext("After completed")
return Disposables.create()
}
.subscribe { print($0) }
.disposed(by: disposeBag)
// next이벤트를 전달하지 않음 -> 어떠한 이벤트도 전달하지 않음 (completed만 전달)
let disposeBag = DisposeBag()
Observable<Void>.empty()
.subscribe { print($0) }
.disposed(by: disposeBag)
=========================================
결과
completed
=========================================
// next 이벤트를 전달하지 않음 -> error만 전달
let disposeBag = DisposeBag()
enum MyError: Error {
case error
}
Observable<Void>.error(MyError.error)
.subscribe { print($0) }
.disposed(by: disposeBag)
=========================================
결과
error(error)
=========================================