just, from
@IBAction func exMap1() {
Observable.just("Hello")
.map { str in "\(str) RxSwift" }
.subscribe(onNext: { str in
print(str)
})
.disposed(by: disposeBag)
}
@IBAction func exFrom1() {
Observable.from(["RxSwift", "In", "4", "Hours"])
.subscribe(onNext: { str in
print(str)
})
.disposed(by: disposeBag)
}
map, flatmap
@IBAction func exMap3() {
Observable.just("800x600")//단순 값은 just!
.map { $0.replacingOccurrences(of: "x", with: "/") }
.map { "https://picsum.photos/\($0)/?random" }
.map { URL(string: $0) }
.filter { $0 != nil }
.map { $0! }
.map { try Data(contentsOf: $0) }
.map { UIImage(data: $0) }
.subscribe(onNext: { image in
self.imageView.image = image
})
.disposed(by: disposeBag)
}
@IBAction func exFlatMap() {
Observable.just("234")
.flatMap({
Observable<String>.just(String($0))
})
.subscribe({ n in
print(n)
})
.disposed(by: disposeBag)
}
filter
@IBAction func exFilter() {
Observable.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])//배열을 쓸 때는 from,
.filter { $0 % 2 == 0 }
.subscribe(onNext: { n in
print(n)
})
.disposed(by: disposeBag)
}
first
@IBAction func exFirst() {
Observable.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])//배열을 쓸 때는 from,
.first()
.subscribe({ n in
print(n)
})
.disposed(by: disposeBag)
}
언제 어떤 operator를 써야할지에 대해 상황별로 나눠준 결정 트리
참고 : https://reactivex.io/documentation/ko/operators.html