Search

RxSwift - Transforming Operator

ย Transforming Operator

Transforming Operator๋Š” Observable์ด ๋‚ด๋ณด๋‚ด๋Š” ์ด๋ฒคํŠธ ํƒ€์ž…์„ ๋ณ€ํ™˜ํ•˜๋Š” ์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค.

toArray

Observable์ด ๋ฐฉ์ถœํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„ ๋ฐฉ์ถœ
toArray๋Š” completed ์ด๋ฒคํŠธ ์ „๊นŒ์ง€ ์ด๋ฒคํŠธ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์•Š๋‹ค๊ฐ€ completed ์‹œ์— ์ด๋ฒคํŠธ๋ฅผ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค.
์›๋ž˜๋Š” Observable๋ฅผ ๋ฆฌํ„ดํ–ˆ๋Š”๋ฐ, RxSwift 5.0๋ถ€ํ„ฐ Single ํƒ€์ž…์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
let disposeBag = DisposeBag() let subject = PublishSubject<Int>() subject .toArray() .subscribe(onSuccess: { print($0) }) .disposed(by: disposeBag) subject.onNext(1) subject.onNext(2) subject.onCompleted() /* [1, 2] */
Swift
๋ณต์‚ฌ

map

Swift์˜ ๊ณ ์ฐจํ•จ์ˆ˜ map๊ณผ ๋™์ผํ•˜๊ฒŒ ๊ฐ๊ฐ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ
map์€ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ „๋‹ฌ๋ฐ›์€ ์š”์†Œ์˜ ํƒ€์ž…๊ณผ ๋ฆฌํ„ดํ•˜๋Š” ์š”์†Œ์˜ ํƒ€์ž…์ด ๊ฐ™์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.
let disposeBag = DisposeBag() let formatter = NumberFormatter() formatter.numberStyle = .spellOut Observable<NSNumber>.of(123, 4, 56) .map { formatter.string(from: $0) ?? "" } .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) /* ๋ฐฑ์ด์‹ญ์‚ผ ์‚ฌ ์˜ค์‹ญ์œก */
Swift
๋ณต์‚ฌ

flatMap

Observable์˜ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์•„์„œ ์ƒˆ๋กœ์šด Observable๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๋ฐฉ์ถœ
์ƒˆ๋กœ์šด Observable์€ ์ƒˆ๋กœ์šด ํƒ€์ž„๋ผ์ธ์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ Observable Sequence๋“ค์€ ํ•˜๋‚˜์˜ Observable Sequence๋กœ ๋ณ‘ํ•ฉ๋ฉ๋‹ˆ๋‹ค.
struct Student { var score: BehaviorSubject<Int> } let disposeBag = DisposeBag() let ryan = Student(score: BehaviorSubject(value: 80)) let charlotte = Student(score: BehaviorSubject(value: 90)) let student = PublishSubject<Student>() student .flatMap { $0.score } .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) student.onNext(ryan) ryan.score.onNext(85) student.onNext(charlotte) ryan.score.onNext(95) charlotte.score.onNext(100) /* 80 85 90 95 100 */
Swift
๋ณต์‚ฌ

flatMapLatest

๊ฐ€์žฅ ์ตœ๊ทผ์— ๋„˜์–ด์˜จ Observable ์ด๋ฒคํŠธ ๊ฐ’๋งŒ ๋„˜๊น€
map๊ณผ switchLatest๋ฅผ ํ•ฉํ•œ ํ˜•ํƒœ๋กœ ํ•ญ๋ชฉ์„ ํ•˜๋‚˜์˜ Observable๋กœ ํ•ฉ์น˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋“ค์–ด์˜จ Observable๋กœ ๋„˜์–ด๊ฐ€๋ฉด์„œ ์ž๋™์ ์œผ๋กœ ์ด์ „ Observable ๊ตฌ๋…์„ ํ•ด์ง€ํ•ฉ๋‹ˆ๋‹ค.
let disposeBag = DisposeBag() let a = BehaviorSubject(value: 1) let b = BehaviorSubject(value: 2) let subject = PublishSubject<BehaviorSubject<Int>>() subject .flatMapLatest { $0.asObservable() } .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) subject.onNext(a) subject.onNext(b) a.onNext(11) b.onNext(22) b.onNext(222) a.onNext(111) /* 1 2 22 222 */
Swift
๋ณต์‚ฌ

flatMapFirst

์ฒ˜์Œ ๊ตฌ๋…์„ ์‹œ์ž‘ํ•œ Observable์˜ ์ด๋ฒคํŠธ๋งŒ ๋„˜๊น€
flatMapLatest์™€ ๋‹ค๋ฅด๊ฒŒ ์ฒ˜์Œ ๊ตฌ๋…์„ ์‹œ์ž‘ํ•œ Observable์˜ ์ด๋ฒคํŠธ๋งŒ ๋„˜๊น๋‹ˆ๋‹ค.
let disposeBag = DisposeBag() let a = BehaviorSubject(value: 1) let b = BehaviorSubject(value: 2) let subject = PublishSubject<BehaviorSubject<Int>>() subject .flatMapFirst { $0.asObservable() } .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) subject.onNext(a) subject.onNext(b) a.onNext(11) b.onNext(22) b.onNext(222) a.onNext(111) /* 1 11 111 */
Swift
๋ณต์‚ฌ

groupBy

์ด๋ฒคํŠธ๋ฅผ ๋ถ„๋ฅ˜ํ•ด์„œ ๋‹ค๋ฅธ Observable๋กœ ๋ณ€ํ˜•
groupBy๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ์š”์†Œ๋“ค์„ ์†์‰ฝ๊ฒŒ Grouping ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
let disposeBag = DisposeBag() let words = ["Apple", "Banana", "Orange", "Book", "City", "Axe"] Observable.from(words) .groupBy { $0.count } .flatMap { $0.toArray() } .subscribe { print($0) } .disposed(by: disposeBag) /* next(["Banana", "Orange"]) next(["Axe"]) next(["Book", "City"]) next(["Apple"]) completed */
Swift
๋ณต์‚ฌ