์ ๋ค๋ฆญ ๊ตฌ์กฐ์ฒด | ๋ค๋ฅธ Publisher๊ฐ ์์๋ฅผ ๋ฐฉ์ถํ๊ธฐ ์ด์ ์ ํ Publisher์ ์์๋ฅผ ๋ชจ๋ ๋ฐฉ์ถํ๋ Publisher
์ด๋์
๋ผ์ด์ ๋ ๋ ๊ฐ์ ์ธ์๋ฅผ ๋ฐ๋๋ค.
prefix
: suffix
๊ฐ ์์๋ฅผ ๋ค์ ๋ฐํํ๊ธฐ ์ด์ ์ ๋ชจ๋ ์์๋ฅผ ๋ค์ ๋ฐํํ๋ Publisher
suffix
: prefix
Publisher๊ฐ ์ข
๋ฃํ ์งํ ๋ค์ ๋ฐํํ๋ Publisher
prefix
Publisher์ suffix
Publisher์ Output ํ์
๊ณผ ์๋ฌ ํ์
์ ๋์ผํด์ผ ํ๋ค.
Publisher๋ฅผ ์ด์ด ๋ถ์ด๊ฑฐ๋ ์์๋ฅผ ์ถ๊ฐํ ๋ ์ฌ์ฉํ ์ ์๋ค.
append
๋ฐ prepend
์คํผ๋ ์ดํฐ์ ๊ด๋ จ์ด ์๋ค.
append
์คํผ๋ ์ดํฐ๋ ๋ค์์ ํํ๋ฅผ ๊ฐ๋๋ค.
๊ฐ๋ณ ์ธ์๋ฅผ ๋ฐ์ ๊ธฐ์กด Publisher๊ฐ ์ข
๋ฃํ ํ ์ด์ด์ ๋ฐํํ๋ค. ex) Just(1).append(2, 3)
์ํ์ค๋ฅผ ๋ฐ์ ๊ธฐ์กด Publisher๊ฐ ์ข
๋ฃํ ํ ์ด์ด์ ๋ฐํํ๋ค. ex) Just(1).append([2, 3])
Publisher๋ฅผ ๋ฐ์ ๊ธฐ์กด Publisher๊ฐ ์ข
๋ฃํ ํ ์ด์ด์ ๋ฐํํ๋ค. ex) Just(1).append(Just(2))
prepend
์คํผ๋ ์ดํฐ๋ ๋ค์์ ํํ๋ฅผ ๊ฐ๋๋ค.
๊ฐ๋ณ ์ธ์๋ฅผ ๋ฐ์ ๊ธฐ์กด Publisher์ ์์ ์ด์ด ๋ถ์ฌ ๋ฐํํ๋ค. ex) Just(1).prepend(2, 3)
์ํ์ค๋ฅผ ๋ฐ์ ๊ธฐ์กด Publisher์ ์์ ์ด์ด ๋ถ์ฌ ๋ฐํํ๋ค. ex) Just(1).prepend([2, 3])
Publisher๋ฅผ ๋ฐ์ ๊ธฐ์กด Publisher์ ์์ ์ด์ด ๋ถ์ฌ ๋ฐํํ๋ค. ex) Just(1).prepend(Just(2))
// Publishers.Concatenate Publisher
Publishers
.Concatenate(prefix: Just(1), suffix: Just(2))
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Concatenate Error")
case .finished:
print("Combine Concatenate Finish")
}
}, receiveValue: { value in
print("Combine Concatenate : \(value)")
})
.store(in: &cancellables)
// append Operator
Just(1)
.append(Just(2))
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Concatenate Error")
case .finished:
print("Combine Concatenate Finish")
}
}, receiveValue: { value in
print("Combine Concatenate : \(value)")
})
.store(in: &cancellables)
// Combine Concatenate : 1
// Combine Concatenate : 2
// Combine Concatenate Finish
// prepend Operator
Just(1)
.prepend(Just(2))
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Concatenate Error")
case .finished:
print("Combine Concatenate Finish")
}
}, receiveValue: { value in
print("Combine Concatenate : \(value)")
})
.store(in: &cancellables)
// Combine Concatenate : 2
// Combine Concatenate : 1
// Combine Concatenate Finish
1๋ฒ ์ฝ๋์ 2๋ฒ ์ฝ๋์ ๊ฒฝ์ฐ 1์ ๊ฐ์ ๋ด๋ Just
Publisher์ ๋์ 2์ ๊ฐ์ ๋ด๋ Just
Publisher๋ฅผ ์ด์ด ๋ถ์๋ค. ๊ฒฐ๊ณผ์ ์ผ๋ก 1๊ณผ 2์ ๊ฐ์ ์ฐจ๋ก๋๋ก ๋ด๊ณ ์ข
๋ฃํ๋ค.
3๋ฒ ์ฝ๋์ ๊ฒฝ์ฐ 1์ ๊ฐ์ ๋ด๋ Just
Publisher์ ์์ 2์ ๊ฐ์ ๋ด๋ Just
Publisher๋ฅผ ์ด์ด ๋ถ์๋ค. ๊ฒฐ๊ณผ์ ์ผ๋ก 2์ 1์ ๊ฐ์ ์ฐจ๋ก๋๋ก ๋ด๊ณ ์ข
๋ฃํ๋ค.
RxSwift
concat
์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ append
์คํผ๋ ์ดํฐ์ ๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์๋ค.
startWith
์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ prepend
์คํผ๋ ์ดํฐ์ ๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์๋ค.
// concat Operator
Observable.just(1)
.concat(Observable.just(2))
.subscribe(onNext: { value in
print("RxSwift Concatenate : \(value)")
}, onError: { _ in
print("RxSwift Concatenate Error")
}, onCompleted: {
print("RxSwift Concatenate Finish")
})
.disposed(by: disposeBag)
// RxSwift Concatenate : 1
// RxSwift Concatenate : 2
// RxSwift Concatenate Finish
// startWith Operator
Observable.just(1)
.startWith(2)
.subscribe(onNext: { value in
print("RxSwift Concatenate : \(value)")
}, onError: { _ in
print("RxSwift Concatenate Error")
}, onCompleted: {
print("RxSwift Concatenate Finish")
})
.disposed(by: disposeBag)
// RxSwift Concatenate : 2
// RxSwift Concatenate : 1
// RxSwift Concatenate Finish
ReactiveSwift
concat
์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ append
์คํผ๋ ์ดํฐ์ ๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์๋ค.
prefix
์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ prepend
์คํผ๋ ์ดํฐ์ ๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์๋ค.
// concat Operator
SignalProducer(value: 1)
.concat(value: 2)
.start { event in
switch event {
case let .value(value):
print("ReactiveSwift Concatenate : \(value)")
case .failed:
print("ReactiveSwift Concatenate Error")
case .completed:
print("ReactiveSwift Concatenate Finish")
default:
break
}
}
// ReactiveSwift Concatenate : 1
// ReactiveSwift Concatenate : 2
// ReactiveSwift Concatenate Finish
// prefix Operator
SignalProducer(value: 1)
.prefix(value: 2)
.start { event in
switch event {
case let .value(value):
print("ReactiveSwift Concatenate : \(value)")
case .failed:
print("ReactiveSwift Concatenate Error")
case .completed:
print("ReactiveSwift Concatenate Finish")
default:
break
}
}
// ReactiveSwift Concatenate : 2
// ReactiveSwift Concatenate : 1
// ReactiveSwift Concatenate Finish
์ฐธ๊ณ
ReactiveX - Operators - StartWith