Observable 생성 연산자에 대해 몇 개 나열해보면서 저도 공부를...해보려고 합니다 !
자주 쓰이고 필요할 것 같은 연산자 위주로 적어보겠습니다.
1. Just
- 가장 기본적인 연산자, 생성된 이벤트를 바로 전달하고 completed 됨
import UIKit
import RxSwift
let just = Observable.just(1)
just.subscribe() { print($0) }
=> next(1)
=> completed
2. Of
- 2개 이상의 이벤트를 전달하는 경우에 쓰임
- from과 구분해야할 것이, of는 Observable<[Int]>를 return하고 from은 배열 안의 요소 각각을 Observable로 return 한다.
import UIKit
import RxSwift
let of = Observable.of(1,2,3,4,5)
of.subscribe() { print($0) }
=> next(1)
=> next(2)
=> next(3)
=> next(4)
=> next(5)
=> completed
3. from
- 배열의 값을 각각 다르게 전달할 때에 쓰이는 연산자
- 위 설명과 마찬가지로 of 연산자는 개별이 아닌 통째로 넘김 (예제 참고)
import UIKit
import RxSwift
let from = Observable.from([1,2,3,4,5])
from.subscribe() { print($0) }
=> next(1)
=> next(2)
=> next(3)
=> next(4)
=> next(5)
=> completed
//-------------------------------------------
let of = Observable.of([1,2,3,4,5])
of.subscribe() { print($0) }
next([1, 2, 3, 4, 5])
completed
4. range
- 시작 값에서부터 지정한 갯수 만큼 1씩 증가시킨 값 각각의 Observable을 생성
import UIKit
import RxSwift
let range = Observable.range(start: 2, count: 4)
range.subscribe() { print($0) }
=> next(2)
=> next(3)
=> next(4)
=> next(5)
=> completed
Rx 공부 제대로 시작해보려하는데, 큭... 저한테 러닝커브가 너무너무 높네요 ㅎㅎㅎㅎ
공부한 것들 조금씩 보완해나가며, 차근차근 적어나가 보겠습니다... 😅 !!
<참고 사이트>
1. reactivex.io/documentation/observable.html
ReactiveX - Observable
Observable In ReactiveX an observer subscribes to an Observable. Then that observer reacts to whatever item or sequence of items the Observable emits. This pattern facilitates concurrent operations because it does not need to block while waiting for the Ob
reactivex.io
'iOS의 모든것' 카테고리의 다른 글
[iOS의 모든것] 자주 쓰는 코코아팟 명령어 (0) | 2021.04.07 |
---|---|
[iOS의 모든것] 애플 개발자 계정 등록 (1) | 2021.04.05 |
[iOS의 모든것] RxSwift - Subject (0) | 2021.03.11 |
[iOS의 모든것] Xcode Project path 한글 (0) | 2020.12.07 |
[iOS의 모든것] Navigation Bar 감추기 (0) | 2020.02.01 |