Search

RxSwift - Filtering Operator

ย Filtering Operator

Filtering Operator๋Š” Observable์˜ item๋ฅผ ์„ ํƒ์ ์œผ๋กœ ๋ฐฉ์ถœํ•ด์ฃผ๋Š” Operator ์ž…๋‹ˆ๋‹ค.
์œ„์˜ ๋ฌธ์žฅ์„ ์ข€ ๋” ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ์šฐ๋ฆฌ๊ฐ€ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” โ€œ์ด๋ชจ์ง€โ€์ฐฝ์„ ๋ณผ๊ฒŒ์š”.
์ด๋ชจ์ง€ ํ™”๋ฉด
์ด๋ชจ์ง€๋Š” ๋„ˆ๋ฌด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์ด๋ชจ์ง€๋ฅผ ์ฐพ๊ณ  ์‹ถ๋‹ค๋ฉด ์ƒ๋‹จ์— ์žˆ๋Š” ๊ฒ€์ƒ‰ ๋ถ€๋ถ„์„ ๋ˆŒ๋Ÿฌ์„œ ์›ํ•˜๋Š” ์ด๋ชจ์ง€ ์ด๋ฆ„์„ ๋„ฃ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ €๋Š” ์šฐ๋Š” ์ด๋ชจ์ง€๋ฅผ ์ฐพ๊ณ  ์‹ถ๊ธฐ ๋•Œ๋ฌธ์— ๋ˆˆ๋ฌผ ์ด๋ผ๊ณ  ๊ฒ€์ƒ‰ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
์šฐ๋Š” ์ด๋ชจ์ง€ ํ™”๋ฉด
์ œ๊ฐ€ ์›ํ•˜๋Š” ์šฐ๋Š” ์ด๋ชจ์ง€๋“ค๋งŒ ์ž”๋œฉ ๋‚˜์™”๋„ค์š”.
๊ฒ€์ƒ‰์ฐฝ์ฒ˜๋Ÿผ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๊ฐ’๋งŒ ๊ฑธ๋Ÿฌ๋‚ด์„œ ๊ฐ€์ ธ๋‹ค ์ฃผ๋Š” ๊ฒƒ์ด Filtering Operator ์ž…๋‹ˆ๋‹ค.
๊ทผ๋ฐ, Operator๋Š” ์ฝ”๋“œ์˜ ์•„๋ฌด ๋ถ€๋ถ„์ด๋‚˜ ์“ฐ๋ฉด ์ ์šฉ์ด ๋˜๋Š”๊ฑด๊ฐ€์š”?
์•„๋‹™๋‹ˆ๋‹ค. Operator๋Š” .next ์ด๋ฒคํŠธ์— ์กฐ๊ฑด๋ถ€ ์ œ์•ฝ ์กฐ๊ฑด์„ ์ ์šฉํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ, ์ด๋ฒคํŠธ๋ฅผ ๋ณด๋‚ด์ค„ Observable๊ณผ ์ด๋ฒคํŠธ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•  ๊ฒƒ์ธ์ง€๊ฐ€ ๋‚˜ํƒ€๋‚˜ ์žˆ๋Š” next ์‚ฌ์ด์— Operator๋ฅผ ๋‘๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ•˜๋‹จ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๋ง์ด์ฃ .
Observable .filter { $0 < 10 } // ์ด ๋ถ€๋ถ„์— ์ž‘์„ฑ .subscribe { print($0) } .disposed(by: bag)
Swift
๋ณต์‚ฌ

ย Filtering Operator์˜ ์ข…๋ฅ˜

Ignoring Operators

ignoreElements

next ์ด๋ฒคํŠธ๋ฅผ ๋ฌด์‹œํ•˜๊ณ  .error๋‚˜ .completed๋ฅผ ์ „๋‹ฌ
ignoreElements๋Š” .error๋‚˜ .completed๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ข…๋ฃŒ๋˜๋Š” ์‹œ์ ๋งŒ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
let hamster = PublishSubject<String>() let bag = DisposeBag() hamster .ignoreElements() .subscribe (onNext: { food in print("ํ–„์Šคํ„ฐ๊ฐ€ ๋จน์€ \(food)") }, onCompleted: { print("ํ–„์Šคํ„ฐ ๋ฐฐ๋ถ€๋ฆ„") }) .disposed(by: bag) hamster.onNext("ํ–„๋ฒ„๊ฑฐ") hamster.onNext("์†Œ์‹œ์ง€") hamster.onNext("ํ–„") hamster.onCompleted() /* ํ–„์Šคํ„ฐ ๋ฐฐ๋ถ€๋ฆ„ */
Swift
๋ณต์‚ฌ

element(at:)

๋ฐœ์ƒํ•œ ์ด๋ฒคํŠธ ์ค‘์— ์ž…๋ ฅํ•œ index๋ฒˆํ˜ธ์˜ ์ด๋ฒคํŠธ๋งŒ ์ˆ˜์‹ 
let hamster = PublishSubject<String>() let bag = DisposeBag() hamster .element(at: 1) .subscribe (onNext: { food in print("ํ–„์Šคํ„ฐ๊ฐ€ ๋จน์€ \(food)") }, onCompleted: { print("ํ–„์Šคํ„ฐ ๋ฐฐ๋ถ€๋ฆ„") }) .disposed(by: bag) hamster.onNext("ํ–„๋ฒ„๊ฑฐ") hamster.onNext("์†Œ์‹œ์ง€") hamster.onNext("ํ–„") hamster.onCompleted() /* ํ–„์Šคํ„ฐ๊ฐ€ ๋จน์€ ์†Œ์‹œ์ง€ ํ–„์Šคํ„ฐ ๋ฐฐ๋ถ€๋ฆ„ */
Swift
๋ณต์‚ฌ

filter

๋ฐฉ์ถœํ•  ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ์กฐ๊ฑด์„ ๊ฑบ
filter๋Š” ๋‚ด๋ถ€์— ์กฐ๊ฑด๋ฌธ์„ ๋ช…์‹œ์ ์œผ๋กœ ๊ฑธ์–ด์„œ ์š”์†Œ๋“ค์„ ํ•„ํ„ฐ๋งํ•ฉ๋‹ˆ๋‹ค.
let books = ["๋ฐ๋ฏธ์•ˆ", "1984", "์ธ๊ฐ„์‹ค๊ฒฉ", "๋™๋ฌผ๋†์žฅ", "๋ชจ๋ชจ"] let bag = DisposeBag() Observable.from(books) .filter { $0.count > 3 } .subscribe (onNext: { book in print("๋‚ด๊ฐ€ ๋ณธ \(book)") }, onCompleted: { print("์ง€์‹ ์ถฉ์ „ ์™„๋ฃŒ") }) .disposed(by: bag) /* ๋‚ด๊ฐ€ ๋ณธ 1984 ๋‚ด๊ฐ€ ๋ณธ ์ธ๊ฐ„์‹ค๊ฒฉ ๋‚ด๊ฐ€ ๋ณธ ๋™๋ฌผ๋†์žฅ ์ง€์‹ ์ถฉ์ „ ์™„๋ฃŒ */
Swift
๋ณต์‚ฌ

Skipping Operators

skip

์ฒ˜์Œ ๋ฐœ์ƒํ•˜๋Š” n(์ž…๋ ฅํ•œ ๊ฐœ์ˆ˜)๊ฐœ์˜ ์ด๋ฒคํŠธ๋ฅผ skip
let books = ["๋ฐ๋ฏธ์•ˆ", "1984", "์ธ๊ฐ„์‹ค๊ฒฉ", "๋™๋ฌผ๋†์žฅ", "๋ชจ๋ชจ"] let bag = DisposeBag() Observable.from(books) .skip(5) .subscribe (onNext: { book in print("๋‚ด๊ฐ€ ๋ณธ \(book)") }, onCompleted: { print("์ง€์‹ ์ถฉ์ „ ์™„๋ฃŒ") }) .disposed(by: bag) /* ์ง€์‹ ์ถฉ์ „ ์™„๋ฃŒ */
Swift
๋ณต์‚ฌ

skip(while:)

ํŠน์ • ์กฐ๊ฑด์ด false์ผ ๋•Œ๊นŒ์ง€ skipํ•˜๊ณ  ๊ทธ ์ดํ›„์˜ ์š”์†Œ๋“ค์€ ๋ฐฉ์ถœ
filter์™€ ๊ธฐ๋Šฅ์ด ์œ ์‚ฌํ•˜์ง€๋งŒ โ“ต ์กฐ๊ฑด์„ ํ†ต๊ณผํ•˜๋Š” ๊ฒฝ์šฐ์— ๊ฐ’์„ skipํ•œ๋‹ค๋Š” ์ ์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. โ“ถ ํ•œ ๋ฒˆ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๊ณ  ๋‚˜๋ฉด ๊ทธ ์ดํ›„์—๋Š” ์กฐ๊ฑด์— ์ƒ๊ด€์—†์ด ๋ชจ๋“  ๊ฐ’์„ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค.
let books = ["๋ฐ๋ฏธ์•ˆ", "1984", "์ธ๊ฐ„์‹ค๊ฒฉ", "๋™๋ฌผ๋†์žฅ", "๋ชจ๋ชจ"] let bag = DisposeBag() Observable.from(books) .skip { $0 != "์ธ๊ฐ„์‹ค๊ฒฉ" } .subscribe (onNext: { book in print("๋‚ด๊ฐ€ ๋ณธ \(book)") }, onCompleted: { print("์ง€์‹ ์ถฉ์ „ ์™„๋ฃŒ") }) .disposed(by: bag) /* ๋‚ด๊ฐ€ ๋ณธ ์ธ๊ฐ„์‹ค๊ฒฉ ๋‚ด๊ฐ€ ๋ณธ ๋™๋ฌผ๋†์žฅ ๋‚ด๊ฐ€ ๋ณธ ๋ชจ๋ชจ ์ง€์‹ ์ถฉ์ „ ์™„๋ฃŒ */
Swift
๋ณต์‚ฌ

skip(until:)

๋‹ค๋ฅธ Observable์—์„œ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ์ „๊นŒ์ง€ ๋ฐœ์ƒํ•œ ์ด๋ฒคํŠธ๋ฅผ skip
let kDaughterHamster = PublishSubject<String>() let kFatherHamster = PublishSubject<String>() let bag = DisposeBag() kDaughterHamster .skip(until: kFatherHamster) .subscribe (onNext: { food in print("๋”ธ๋ž‘๊ตฌ ํ–„์Šคํ„ฐ๊ฐ€ ๋จน์€ \(food)") }, onCompleted: { print("๋”ธ๋ž‘๊ตฌ ํ–„์Šคํ„ฐ ๋ฐฐ๋ถ€๋ฆ„") }).disposed(by: bag) kDaughterHamster.onNext("ํ–„๋ฒ„๊ฑฐ") kDaughterHamster.onNext("์†Œ์‹œ์ง€") kDaughterHamster.onNext("ํ–„") kFatherHamster.onNext("์‚ผ๊ฒน์‚ด") kDaughterHamster.onNext("๋‚˜๋ฌผ") // ์œ ๊ต๊ฑธ ๋”ธ๋ž‘๊ตฌ ํ–„์Šคํ„ฐ๋Š” ์•„๋น  ํ–„์Šคํ„ฐ๊ฐ€ ์ˆ˜์ €๋ฅผ ๋“ค๊ธฐ ์ „๊นŒ์ง€ ์Œ์‹์„ ๋จน์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. /* ๋”ธ๋ž‘๊ตฌ ํ–„์Šคํ„ฐ๊ฐ€ ๋จน์€ ๋‚˜๋ฌผ */
Swift
๋ณต์‚ฌ

Taking Operators

take

์ž…๋ ฅํ•œ ๊ฐœ์ˆ˜๋งŒํผ์˜ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›๊ณ  ๋‚˜๋จธ์ง€๋Š” ๋ฌด์‹œ
skip๊ณผ๋Š” ๋ฐ˜๋Œ€๋กœ ์ฒ˜์Œ n(์ž…๋ ฅํ•œ ๊ฐœ์ˆ˜)๊ฐœ์˜ ์š”์†Œ๋“ค๋งŒ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค.
let books = ["๋ฐ๋ฏธ์•ˆ", "1984", "์ธ๊ฐ„์‹ค๊ฒฉ", "๋™๋ฌผ๋†์žฅ", "๋ชจ๋ชจ"] let bag = DisposeBag() Observable.from(books) .take(3) .subscribe (onNext: { book in print("๋‚ด๊ฐ€ ๋ณธ \(book)") }, onCompleted: { print("์ง€์‹ ์ถฉ์ „ ์™„๋ฃŒ") }) .disposed(by: bag) /* ๋‚ด๊ฐ€ ๋ณธ ๋ฐ๋ฏธ์•ˆ ๋‚ด๊ฐ€ ๋ณธ 1984 ๋‚ด๊ฐ€ ๋ณธ ์ธ๊ฐ„์‹ค๊ฒฉ ์ง€์‹ ์ถฉ์ „ ์™„๋ฃŒ */
Swift
๋ณต์‚ฌ

take(while:)

ํŠน์ •์กฐ๊ฑด์ด false์ผ ๋•Œ๊นŒ์ง€ ์ด๋ฒคํŠธ๋ฅผ ๋ฐฉ์ถœ
skip(while:)๊ณผ๋Š” ๋ฐ˜๋Œ€๋กœ ํŠน์ • ์กฐ๊ฑด์ด false ์ผ ๋•Œ๊นŒ์ง€๋งŒ ์ด๋ฒคํŠธ๋ฅผ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค.
let books = ["๋ฐ๋ฏธ์•ˆ", "1984", "์ธ๊ฐ„์‹ค๊ฒฉ", "๋™๋ฌผ๋†์žฅ", "๋ชจ๋ชจ"] let bag = DisposeBag() Observable.from(books) .take(while: { $0.count < 4 }) .subscribe (onNext: { book in print("๋‚ด๊ฐ€ ๋ณธ \(book)") }, onCompleted: { print("์ง€์‹ ์ถฉ์ „ ์™„๋ฃŒ") }) .disposed(by: bag) /* ๋‚ด๊ฐ€ ๋ณธ ๋ฐ๋ฏธ์•ˆ ์ง€์‹ ์ถฉ์ „ ์™„๋ฃŒ */
Swift
๋ณต์‚ฌ

take(until:)

๋‹ค๋ฅธ Observable์—์„œ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ์ „๊นŒ์ง€ ์ด๋ฒคํŠธ๋ฅผ ๋ฐฉ์ถœ
skip(until:)๊ณผ๋Š” ๋ฐ˜๋Œ€๋กœ ๋‹ค๋ฅธ Observable์—์„œ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋”์ด์ƒ ์ด๋ฒคํŠธ๋ฅผ ๋ฐฉ์ถœํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
let hamster๋™๋ฌด = PublishSubject<String>() let trigger = PublishSubject<String>() let disposeBag = DisposeBag() hamster๋™๋ฌด .take(until: trigger) .subscribe(onNext: { work in print("์ผํ•˜์ž ํ–„์Šคํ„ฐ ๋™๋ฌด, ํ•˜์ž \(work)") }).disposed(by: disposeBag) hamster๋™๋ฌด.onNext("๋ฐ”๋Š์งˆ") hamster๋™๋ฌด.onNext("๊ณต์žฅ์ผ") trigger.onNext("ํ–„์Šคํ„ฐ ๋™๋ฌด!! ๊ตญ๊ธฐ์— ๋Œ€ํ•œ ๊ฒฝ๋ก€๋ฅผ ํ•˜์‹œ๊ฒŒ!!") hamster๋™๋ฌด.onNext("์ฝ”๋”ฉ") /* ์ผํ•˜์ž ํ–„์Šคํ„ฐ ๋™๋ฌด, ํ•˜์ž ๋ฐ”๋Š์งˆ ์ผํ•˜์ž ํ–„์Šคํ„ฐ ๋™๋ฌด, ํ•˜์ž ๊ณต์žฅ์ผ */
Swift
๋ณต์‚ฌ

takeLast

์™„๋ฃŒ๊ฐ€ ๋˜๋ฉด ์ž…๋ ฅํ•œ ๊ฐœ์ˆ˜๋งŒํผ ์ด์ „์˜ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐฉ์ถœ
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] let subject = PublishSubject<Int>() let disposeBag = DisposeBag() subject .takeLast(2) .subscribe { print($0) } .disposed(by: disposeBag) numbers.forEach { subject.onNext($0) } subject.onNext(11) subject.onCompleted() /* next(10) next(11) completed */
Swift
๋ณต์‚ฌ

Distinct Operators

distinctUntilChanged

์—ฐ๋‹ฌ์•„ ๋‚˜์˜ค๋Š” ์ค‘๋ณต ์š”์†Œ ๋ฌด์‹œ
let disposeBag = DisposeBag() Observable.of("๋”ธ๊ธฐ", "๋‹น๊ทผ", "๋‹น๊ทผ", "์ˆ˜๋ฐ•", "์ˆ˜๋ฐ•", "๋ฉ”๋ก ", "์ˆ˜๋ฐ•") .distinctUntilChanged() .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) /* ๋”ธ๊ธฐ ๋‹น๊ทผ ์ˆ˜๋ฐ• ๋ฉ”๋ก  ์ˆ˜๋ฐ• */
Swift
๋ณต์‚ฌ